#P16133. [ICPC 2018 NAIPC] Rainbow Graph
[ICPC 2018 NAIPC] Rainbow Graph
题目描述
Roy and Biv have an undirected graph with nodes, numbered . Each edge of the graph has a weight and a color. Weights are positive integers. There are exactly three colors of edges: Red, Green, and Blue. There may be multiple edges between the same two vertices, and there may even be self-loops in the graph.
For a fixed positive integer , consider the following task: Roy and Biv want to choose exactly edges of their graph and erase all other edges. They want to do this in such a way that when they look at the graph with just the edges they have chosen, they will both see that the entire graph is connected.
However, there is a catch: Roy cannot see the color Red and Biv cannot see the color Blue. Therefore, they have to choose the edges in such a way that the Blue and Green edges are enough to connect all nodes, and also the Red and Green edges are enough to connect all nodes. What is the minimum combined weights for all of the edges which will allow both Roy and Biv to see the graph as connected, for all values of from 1 to the total number of edges?
输入格式
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will begin with a line with two integers, and (), where is the number of nodes in the graph, and is the number of edges.
Each of the next lines will contain three integers and a character, , (), () and (), which represents an edge between nodes and with weight , and color ( Red, Green, Blue).
输出格式
Output lines, with each line representing a result for in order. If it is not possible for both Roy and Biv to see the graph as connected, output for that value of . Otherwise, output the minimum sum of edge weights for a subset of edges which will allow both Roy and Biv to see the graph as connected.
5 8
1 5 1 R
2 1 2 R
5 4 5 R
4 5 3 G
1 3 3 G
4 3 5 G
5 4 1 B
1 2 2 B
-1
-1
-1
-1
15
14
17
22