eolymp
bolt
Try our new interface for solving problems
Problems

Two Famous Companies

Two Famous Companies

In China, there are two companies offering the Internet service for the people from all cities: China Telecom and China Unicom. They both are planning to build cables between cities. Obviously, the government wants to connect all the cities in minimum costs. So the minister of finance Mr. B wants to choose some of the cable plans from the two companies and calculate the minimum cost needed to connect all the cities. Mr. B knows that \textbf{N-1} cables should be built in order to connect all \textbf{N} cities of China. For some honorable reason, Mr. B should choose \textbf{K} cables from the China Telecom and the rest \textbf{N-1-K} cables from the China Unicom. Your job is to help Mr. B determine which cables should be built and the minimum cost to build them. You may assume that the solution always exists. \InputFile Each test case starts with a line containing the number of cities \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{50000}), number of cable plans \textbf{M} (\textbf{N-1} ≤ \textbf{M} ≤ \textbf{100000}) and the number of required cables from China Telecom \textbf{K} (\textbf{0} ≤ \textbf{K} ≤ \textbf{N-1}). This is followed by \textbf{M} lines, each containing four integers \textbf{a}, \textbf{b}, \textbf{c}, \textbf{x} (\textbf{0} ≤ \textbf{a}, \textbf{b} ≤ \textbf{N-1}, \textbf{a != b}, \textbf{1} ≤ \textbf{c} ≤ \textbf{100}, \textbf{x} in \{\textbf{0}, \textbf{1}\} indicating the pair of cities this cable will connect, the cost to build this cable and the company this cable plan belongs to. \textbf{x=0} denotes that the cable plan belongs to China Telecom and x=1 denotes that the cable plan is from China Unicom. \OutputFile For each test case, display the case number and the minimum cost of the cable building. \Note In the first case, there are two cable plans between the only two cities, one from China Telecom and one from China Unicom. Mr. B needs to choose the one from China Telecom to satisfy the problem requirement even the cost is higher. In the second case, Mr. B must choose the cable from China Unicom, which leads the answer to \textbf{1}.
Time limit 15 seconds
Memory limit 32 MiB
Input example #1
2 2 1
0 1 1 1
0 1 2 0
2 2 0
0 1 1 1
0 1 2 0
Output example #1
Case 1: 2
Case 2: 1
Source ACM ICPC Fudan Local Programming Contest 2012