Connect the Cities
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 15569 Accepted Submission (s): 4108
Problem Description in 2100, since the sea level rise, most of the cities disappear. Though Some survived cities is 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.
Inputthe First line contains the number of test cases.
Each test case is starts with three integers:n, M and K. N (3 <= n <=500) stands for the number of survived cities, m (0 <= M <= 25000) stands for the number of roads you can choose to connect the cities and K (0 <= K <=) St ANDs for the number of still connected cities.
To make it easy, the cities is signed from 1 to N.
Then follow m lines, each contains three integers p, Q and C (0 <= C <=), means it takes C to connect P and Q.
Then follow k lines, each line starts with a 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, the output of the least money is need to take, if it's impossible, just output-1.
Sample Input16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6
Sample OUTPUT1 Test Instructions: Multiple sets of test cases, the first row of each group is n M K, representing the information of N cities, M-roads, and the information of K cities that have been connected. The next M-line is the cost to build the city A to B. Then the first number of K lines per line is T, and then the number of T represents the city that is already connected. Connect between cities on an already connected basis to solve the minimum cost of all unicom. Unable to connect the output-1 typical Kruskal algorithm, and the search set and greedy algorithm to solve the minimum spanning tree, and finally determine whether the city-generated tree is unique. This question is very strange, with Java dead and alive Tle, and then write in C + +, and then the time-out, and then the heuristic search what add in also useless, finally changed to g++ on the past. It's over.
#include <stdio.h>#include<algorithm>#include<string.h>using namespacestd;intfather[505];intdep[505];structseg{intX,y,len;} seg[25010];intCMP (Seg a,seg b) {returnA.len <B.len;}int_find (intx) { if(X==father[x])returnx; return_find (father[x]);}intMain () {inttcase; scanf ("%d",&tcase); while(tcase--) { intn,m,t; scanf ("%d%d%d",&n,&m,&t); for(intI=1; i<=n; i++) {Father[i]=i; Dep[i]=0; } for(intI=0; i<m; i++) { intx, Y, Z scanf ("%d%d%d",&x,&y,&z); seg[i].x=x; Seg[i].y=y; Seg[i].len=Z; } for(intI=0; i<t; i++) { intnum,x; scanf ("%d%d",&num,&x); Num--; while(num--) { inty; scanf ("%d",&y); X=_find (x); Y=_find (y); if(x!=y) {if(dep[x]==Dep[y]) {Father[x]=y; Dep[y]++; } Else if(dep[x]<Dep[y]) {Father[x]=y; } Else{Father[y]=x; }}}} sort (Seg,seg+m,cmp); intsum=0; for(intI=0; i<m; i++) { intx =_find (seg[i].x); inty =_find (SEG[I].Y); if(x!=y) {if(dep[x]==Dep[y]) {Father[x]=y; Dep[y]++; } Else if(dep[x]<Dep[y]) {Father[x]=y; } Else{Father[y]=x; } Sum+=Seg[i].len; } } intans=0; for(intI=1; i<=n; i++) { if(father[i]==i) ans++; } if(ans==1) printf ("%d\n", sum); Elseprintf"-1\n"); } return 0;}
Hdu 3371 (Kruskal)