HDU3371 Connect the Cities 解题报告--prim
来源:互联网 发布:安卓手机数据恢复系统 编辑:程序博客网 时间:2024/06/05 21:52
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6549 Accepted Submission(s): 1884
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
#include<iostream>#define inf 0xffffusing namespace std;int map[700][700];int n,m;int flag;int prim(){int sum=0;int lowcost[1001];int mark[1001];memset(mark,0,sizeof(mark));for(int i=0;i<n;i++){lowcost[i]=map[0][i];}mark[0]=1;int pos;for(i=1;i<n;i++){int Min=inf;for(int j=0;j<n;j++){if(Min>lowcost[j]&&mark[j]==0){Min=lowcost[j];pos=j;}}if(Min==inf) {return -1;break;}mark[pos]=1;sum+=Min;for(j=0;j<n;j++){if(lowcost[j]>map[pos][j]&&mark[j]==0){lowcost[j]=map[pos][j];}}}return sum;}int main(){int k;int r;scanf("%d",&r);while(r--){scanf("%d%d%d",&n,&m,&k);for(int i=0;i<n;i++){for(int j=0;j<n;j++){map[i][j]=inf;}}for(i=0;i<m;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(c<map[b-1][a-1])map[b-1][a-1]=map[a-1][b-1]=c;}int j;for(i=0;i<k;i++){int kk;int orz[1001];scanf("%d",&kk);for(j=0;j<kk;j++)scanf("%d",&orz[j]);for(j=0;j<kk;j++)for(int p=0;p<kk;p++)map[orz[j]-1][orz[p]-1]=0;}printf("%d\n",prim());}return 0;}
- HDU3371 Connect the Cities 解题报告--prim
- hdu3371 Connect the Cities
- HDU3371--Connect the Cities
- hdu3371 Connect the Cities
- HDU3371 Connect the Cities
- hdu3371: Connect the Cities
- HDU3371--- Connect the Cities
- HDU3371-Connect the Cities
- hdu3371 Connect the Cities
- HDU3371 Connect the Cities【Kruskal】
- hdu3371 Connect the Cities (MST)
- hdu3371 Connect the Cities--kruskal
- Connect the Cities(Prim)
- hdu3371——Connect the Cities
- hdu3371 Connect the Cities (最小生成树)
- HDU3371——Connect the Cities
- HDU3371(Connect the Cities)最小生成树
- HDU3371 Connect the Cities 【最小生成树Kruskal】
- hdu 2030 汉字统计
- UVA 10817 Headmaster's Headache
- linux命令大全10
- git 回滚错误的pull origin操作
- hdu 4638 Group (树状数组+离线)
- HDU3371 Connect the Cities 解题报告--prim
- POJ 3678 - Katu Puzzle 比较典型的构图2-sat...求是否有可行解
- 系统二级运维之业务单据错误处理
- 【jquery mobile笔记二】jquery mobile调用豆瓣api示例
- hdu 1712 ACboy needs your help (分组背包)
- 【树形DP】 HDU 4118 Holiday's Accommodation
- 跟我学ios 开发 (一)
- 已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10()
- HDU1233 还是畅通工程 解题报告--prim