#P16133. [ICPC 2018 NAIPC] Rainbow Graph

[ICPC 2018 NAIPC] Rainbow Graph

题目描述

Roy and Biv have an undirected graph with nn nodes, numbered 1n1\dots n. 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 kk, consider the following task: Roy and Biv want to choose exactly kk 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 kk 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 kk 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, nn and mm (1n,m1001 \leq n, m \leq 100), where nn is the number of nodes in the graph, and mm is the number of edges.

Each of the next mm lines will contain three integers and a character, aa, bb (1a,bn1 \leq a, b \leq n), ww (1w1,0001 \leq w \leq 1{,}000) and cc (c{R,G,B}c \in \{R, G, B\}), which represents an edge between nodes aa and bb with weight ww, and color cc (R=R= Red, G=G= Green, B=B= Blue).

输出格式

Output mm lines, with each line representing a result for k=1mk = 1\dots m in order. If it is not possible for both Roy and Biv to see the graph as connected, output 1-1 for that value of kk. Otherwise, output the minimum sum of edge weights for a subset of kk 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