码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3371 Connect the Cities

时间:2017-03-07 22:34:37      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:道路   ota   height   stand   from   get   0ms   name   others   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18733    Accepted Submission(s): 4563


Problem Description
In 2100, since the sea level rise, most of the cities disappear. Though some survived 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.  
 

 

Input
The 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 survived 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.
 

 

Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
 

 

Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
 

 

Sample Output
1
 
题目大意:有n个城市,因为海平面上升,毁坏了一些城市之间的道路,需要重新修建一些道路保持城市之间的畅通。
     给出一些可以选择的道路以及花费,然后再给出一些依旧完好无损的道路。问是否能够保持所有城市之间的
     畅通,以及最少花费是多少。
 
解题思路:用kruskal算法解决,建图的时候注意完好无损的路就好。
 
AC代码:

20022026 2017-03-06 19:52:45 Accepted 3371 920MS 1696K 1381 B G++

1
#include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct point 8 { 9 int x,y,l; 10 }p[25010]; 11 int parent[510],n,m,k; 12 bool cmp(point a, point b) 13 { 14 return a.l < b.l; 15 } 16 int find(int x) 17 { 18 int s,tmp; 19 for (s = x; parent[s] >= 0; s = parent[s]); 20 while (s != x) 21 { 22 tmp = parent[x]; 23 parent[x] = s; 24 x = tmp; 25 } 26 return s; 27 } 28 void Union(int A, int B) 29 { 30 int a = find(A), b = find(B); 31 int tmp = parent[a] + parent[b]; 32 if (parent[a] < parent[b]) 33 { 34 parent[b] = a; 35 parent[a] = tmp; 36 } 37 else 38 { 39 parent[a] = b; 40 parent[b] = tmp; 41 } 42 } 43 void kruskal() 44 { 45 int i,u,v,sum = 0,j = 0; 46 for (i = 1; i <= n; i ++) 47 parent[i] = -1; 48 for (i = 0; i < m; i ++) 49 { 50 u = p[i].x; v = p[i].y; 51 if (find(u) != find(v)) 52 { 53 sum += p[i].l; 54 Union(u,v); 55 j ++; 56 } 57 if (j == n-1) break; 58 } 59 if (j == n-1) 60 printf("%d\n",sum); 61 else 62 printf("-1\n"); 63 } 64 int main () 65 { 66 int i,j,t,d; 67 int f[510]; 68 scanf("%d",&t); 69 while (t --) 70 { 71 scanf("%d%d%d",&n,&m,&k); 72 for (i = 0; i < m; i ++) 73 scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].l); 74 while (k --) 75 { 76 scanf("%d",&d); 77 for (i = 0; i < d; i ++) 78 scanf("%d",&f[i]); 79 for (i = 0; i < d-1; i ++) 80 { 81 p[m].x = f[i]; 82 p[m].y = f[i+1]; 83 p[m].l = 0; 84 m ++; 85 } 86 } 87 sort(p,p+m,cmp); 88 kruskal(); 89 } 90 return 0; 91 }

 

hdu 3371 Connect the Cities

标签:道路   ota   height   stand   from   get   0ms   name   others   

原文地址:http://www.cnblogs.com/yoke/p/6517085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!