HDU 3371
来源:互联网 发布:java 多态 重载 编辑:程序博客网 时间:2024/06/11 01:49
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7040 Accepted Submission(s): 2008
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.
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
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6
Sample Output
1
Author
dandelion
Source
HDOJ Monthly Contest – 2010.04.04
Recommend
lcy
题意是说有n个城市,其中有m条边未建,然后还有已经建好的。让你求最少费用。
一开始用prime做超时,以为算法不行,便用Kruskal做了一遍也是超时。最后才知道原来要用C++交,而不是G++
#include<stdio.h>#include<string.h>#define max 10000000int map[505][505];int lowcost[505];int visit[505];int n;int prime(){ int i,j; int min,pos,sum=0; pos=1; memset(visit,0,sizeof(visit)); for(i=1; i<=n; i++) { lowcost[i]=map[1][i]; } visit[1]=1; for( i=1; i<n; i++) { min=max; for( j=2; j<=n; j++)j=1也可 if(!visit[j]&&min>lowcost[j]) { min=lowcost[j]; pos=j; } if(min==max) { return -1; } visit[pos]=1; sum=sum+min; for( j=2; j<=n; j++)j=1也可 { if(!visit[j]&&lowcost[j]>map[pos][j]) lowcost[j]=map[pos][j]; } } return sum;}int main(){ int te; int m,k; int i,j; int p,q,c; int t; int temp[505]; scanf("%d",&te); while(te--) { scanf("%d%d%d",&n,&m,&k); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { map[i][j]=max; map[i][i]=0; } } while(m--) { scanf("%d%d%d",&p,&q,&c); if(map[p][q]>c) map[p][q]=map[q][p]=c; } while(k--) { scanf("%d",&t); for(i=0;i<t;i++) { scanf("%d",&temp[i]); } for(i=0;i<t;i++) { for(j=i+1;j<t;j++) { map[temp[i]][temp[j]]=map[temp[j]][temp[i]]=0; } } } int result=prime(); if(result==-1) printf("-1\n"); else printf("%d\n",result);}}
复制去Google翻译翻译结果
} } } 素();
}
}
}
}
0 1
- HDU 3371 && HDU 3367
- hdu 3371
- hdu 3371
- HDU 3371
- HDU 3371 Kruskal
- hdu 3371
- hdu 3371 (Prim)
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- 东软实训--PL/SQL基础
- java中的参数传递方式
- Flexgrid 取记录的方式
- Pentaho的印度蒙德里安Pentaho的亚太地区
- 02-4. BCD解密(10)
- HDU 3371
- Android的生命周期小结
- JAVA对多线程的两个有用的辅助类(CountDownLatch和AtomicBoolean)
- 如何使用Disruptor(一)Ringbuffer的特别
- ViewPager的双层嵌套时影响内部ViewPager的触摸滑动问题
- P1000 Galaxy Tab的概述
- QT5.3.1 + mingw4.8 调用vs2008的动态库
- 三年一小结
- Oracle解决方案和Oracle应用开发的支持延伸到开源技术