Connect the citiestime limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 9856 accepted submission (s): 2800
Problem descriptionin 2100, since the sea level rise, most of the cities disappear. though some have ved cities are still connected with others, but most of them become disconnected. the government wants to build some roads to connect all of these cities again, but they don't want to take too much money.
Inputthe first line contains the number of test cases.
Each test case starts with three integers: n, m and K. N (3 <= n <= 500) stands for the number of attached ved cities, M (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and K (0 <= k <= 100) stands for the number of still connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow M lines, each contains three integers p, q and C (0 <= C <= 1000), means it takes C to connect p and q.
Then follow K lines, each line starts with an integer T (2 <= T <= N) stands for the number of this connected cities. then T integers follow stands for the ID of these cities.
Outputfor each case, output the least money you need to take, if it's impossible, just output-1.
Sample Input
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6
Sample output
1
Ah, it took me half a day to get crazy about this question. The first time I used Kruskal to solve the problem, I would like to summarize this algorithm: Kruskal is edge-centered, and every greedy choice of the shortest side to build a minimum spanning tree, in addition, the query set is initialized to N trees at the beginning, corresponding to N nodes. Each time an edge is read, the start and end points of the edge are determined to be in the same tree, if not, the edge cannot be read; otherwise, the ring is formed. If not the same tree, the two shards are merged, and the variable of the total number of record trees is reduced by one, if the variable is equal to 1, the Minimum Spanning Tree is found. If the variable is still not equal to 1 After traversing all edges, there is no minimum spanning tree. Given N points, M points, and t connected sets, find the Minimum Spanning Tree. Problem: As mentioned above, pay attention to special situations. For example, if the tree is connected at the beginning, I am stuck here for eight times @ [email protected]
#include <stdio.h>#include <string.h>#include <stdlib.h>#define maxn 502#define maxm 25002int id, pre[maxn];int count, ans;struct Node{int from, to, val;} edge[maxm];void addEdge(int a, int b, int c){edge[id].from = a;edge[id].to = b;edge[id++].val = c;}int unionFind(int k){int a = k;while(pre[k] != -1) k = pre[k];int b;while(a != k){b = pre[a];pre[a] = k;a = b;}return k;}int cmp(const void* a, const void* b){return ((Node *)a)->val - ((Node *)b)->val;}bool kruskal(){qsort(edge, id, sizeof(Node), cmp);int x, y, i;for(i = 0; i < id; ++i){x = unionFind(edge[i].from);y = unionFind(edge[i].to);if(x != y){ans += edge[i].val;--count; pre[y] = x;if(1 == count) return true;}}return 1 == count;}int main(){//freopen("stdin.txt", "r", stdin);int cas, n, m, k, t, a, b, c, i;scanf("%d", &cas);while(cas--){memset(pre, -1, sizeof(pre));scanf("%d%d%d", &n, &m, &k);for(i = id = 0; i < m; ++i){scanf("%d%d%d", &a, &b, &c);addEdge(a, b, c);}count = n; ans = 0;for(i = 0; i < k; ++i){scanf("%d%d", &t, &a);a = unionFind(a);while(--t){scanf("%d", &b);b = unionFind(b);if(a != b){pre[b] = a;--count;}}}if(!kruskal()) printf("-1\n");else printf("%d\n", ans);}return 0;}