1041 - Road Construction
You are given the description of roads. Damaged roads are formatted as "city1 city2 cost" and non-damaged roads are formatted as "city1 city2 0". In this notation city1 and city2 are the case-sensitive names of the two cities directly connected by that road. If the road is damaged, cost represents the price of rebuilding that road. Every city in the country will appear at least once in roads. And there can be multiple roads between same pair of cities.There are several cities in the country, and some of them are connected by bidirectional roads. Unfortunately, some of the roads are damaged and cannot be used right now. Your goal is to rebuild enough of the damaged roads that there is a functional path between every pair of cities.
Your task is to find the minimum cost of the roads that must be rebuilt to achieve your goal. If it is impossible to do so, print "Impossible".
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case begins with a blank line and an integer m (1 ≤ m ≤ 50) denoting the number of roads. Then there will be m lines, each containing the description of a road. No names will contain more than 50 characters. The road costs will lie in the range [0, 1000].
Output
For each case of input you have to print the case number and the desired result.
Sample Input |
Output for Sample Input |
212Dhaka Sylhet 0
Ctg Dhaka 0 Sylhet Chandpur 9 Ctg Barisal 9 Ctg Rajshahi 9 Dhaka Sylhet 9 Ctg Rajshahi 3 Sylhet Chandpur 5 Khulna Rangpur 7 Chandpur Rangpur 7 Dhaka Rajshahi 6 Dhaka Rajshahi 7
2 Rajshahi Khulna 4 Kushtia Bhola 1 |
Case 1: 31Case 2: Impossible |
题目类型:MST
算法分析:读入字符串然后将字符串转化为数字标识,然后调用kruskal算法直接计算即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
#include <iostream> #include <fstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int maxn = 3600; int parent[maxn]; struct Node { int u, v, w; }; Node edge[maxn]; int len; vector <string> vec; int UnFind (int val) { if (parent[val] == val) return val; return parent[val] = UnFind (parent[val]); } bool Cmp (Node a, Node b) { return a.w < b.w; } int GetVal (string val) { int i; for (i = 0; i < vec.size (); i++) if (val == vec[i]) return i; vec.push_back (val); return i; } void kruskal () { int i, sum = 0; for (i = 0; i < len; i++) { if (UnFind (edge[i].u) != UnFind(edge[i].v)) { parent[UnFind(edge[i].u)] = UnFind (edge[i].v); sum += edge[i].w; } } int cnt = 0; for (i = 0; i < vec.size (); i++) { if (parent[i] == i) { cnt++; if (cnt > 1) { cout << "Impossible" << endl; return ; } } } cout << sum << endl; } int main() { // ifstream cin ("aaa.txt"); int t, flag = 1; cin >> t; while (t--) { int m; string val_a, val_b; int val; cin >> m; int i; for (i = 0; i < maxn; i++) parent[i] = i; len = 0; vec.clear (); for (i = 0; i < m; i++) { cin >> val_a >> val_b >> val; int u = GetVal (val_a); int v = GetVal (val_b); if (val == 0) { parent[UnFind(u)] = UnFind (v); } else { edge[len].u = u; edge[len].v = v; edge[len++].w = val; } } sort (edge, edge + len, Cmp); cout << "Case " << flag++ << ": "; kruskal (); } return 0; } |
- « 上一篇:lightoj1040
- lightoj1042:下一篇 »