Description
Input
Output
1 #include <cstring> 2 #include <string> 3 #include <iostream> 4 #include <cstdio> 5 #include <map> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 25; 10 const int INF = 0x7f7f7f7f; 11 12 struct Node { 13 int u, v, c, use; 14 Node() {} 15 Node(int uu, int vv, int cc): u(uu), v(vv), c(cc), use(false) {} 16 bool operator < (const Node &rhs) const { 17 return c < rhs.c; 18 } 19 }; 20 21 map<string, int> mymap; 22 string s1, s2; 23 int n, m, k, ecnt, ans, cnt; 24 Node *p; 25 int fa[MAXN], head[MAXN], *next; 26 int mat[MAXN][MAXN]; 27 28 void init() { 29 ecnt = n = 0; 30 p = new Node[2 * m + 5]; 31 next = new int[2 * m + 5]; 32 mymap.clear(); 33 mymap["Park"] = n++; 34 memset(mat, 0, sizeof(mat)); 35 } 36 37 int get_set(int x) { 38 return fa[x] == x ? x : get_set(fa[x]); 39 } 40 41 void add_edge(int u, int v, int c) { 42 p[ecnt++] = Node(u, v, c); 43 p[ecnt++] = Node(v, u, c); 44 if(mat[u][v] == 0 || c < mat[u][v]) 45 mat[u][v] = mat[v][u] = c; 46 } 47 48 void build_link() { 49 memset(head, -1, sizeof(head)); 50 for(int i = ecnt - 1; i >= 0; --i) { 51 next[i] = head[p[i].u]; 52 head[p[i].u] = i; 53 } 54 } 55 56 void kruskal_del0() { 57 ans = cnt = 0; 58 for(int i = 0; i < ecnt; ++i) { 59 if(p[i].u == 0 || p[i].v == 0) continue; 60 int x = get_set(p[i].u), y = get_set(p[i].v); 61 if(x == y) continue; 62 fa[x] = y; 63 p[i].use = p[i ^ 1].use = true; 64 ans += p[i].c; 65 ++cnt; 66 } 67 m = n - 1 - cnt; 68 build_link(); 69 for(int i = head[0]; i != -1; i = next[i]) { 70 if(p[i].u && p[i].v) continue; 71 int x = get_set(p[i].u), y = get_set(p[i].v); 72 if(x == y) continue; 73 fa[x] = fa[y] = 0; 74 p[i].use = p[i ^ 1].use = true; 75 ans += p[i].c; 76 if(++cnt == n - 1) break; 77 } 78 } 79 80 void dfs(int x) { 81 for(int i = head[x]; i != -1; i = next[i]) { 82 if(p[i].use) { 83 fa[p[i].v] = x; 84 p[i].use = p[i ^ 1].use = false; 85 dfs(p[i].v); 86 } 87 } 88 } 89 90 int best[MAXN]; 91 92 int get_best(int x) { 93 if(fa[x] == 0) return -1; 94 if(best[x] != -1) return best[x]; 95 return best[x] = max(mat[x][fa[x]], get_best(fa[x])); 96 } 97 98 void exchange_edge() { 99 while(m++ < k) { 100 memset(best, -1, sizeof(best)); 101 for(int i = 0; i < n; ++i) get_best(i); 102 int a = INF, y = 0; 103 for(int i = head[0]; i != -1; i = next[i]) { 104 if(best[p[i].v] != -1 && a > p[i].c - best[p[i].v]) { 105 a = p[i].c - best[p[i].v]; 106 y = p[i].v; 107 } 108 } 109 if(a >= 0) return ; 110 ans += a; fa[y] = 0; 111 } 112 } 113 114 int main() { 115 int c; 116 while(scanf("%d", &m) != EOF) { 117 init(); 118 while(m--) { 119 cin>>s1>>s2>>c; 120 if(mymap.find(s1) == mymap.end()) mymap[s1] = n++; 121 if(mymap.find(s2) == mymap.end()) mymap[s2] = n++; 122 add_edge(mymap[s1], mymap[s2], c); 123 } 124 scanf("%d", &k); 125 for(int i = 0; i < n; ++i) fa[i] = i; 126 sort(p, p + ecnt); 127 kruskal_del0(); 128 dfs(0); 129 exchange_edge(); 130 printf("Total miles driven: %d\n", ans); 131 delete [] p; 132 delete [] next; 133 } 134 }