Connect the Cities 3371
来源:互联网 发布:php清除cookie 编辑:程序博客网 时间:2024/06/05 07:18
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13643 Accepted Submission(s): 3687
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/*法一prim算法*/#include<stdio.h>#include<string.h>#define mx 0x3f3f3fint g[510][510],a[510];int n,m,t;void prim(){int v,i,j,k,min,sum=0;int dis[510],vis[510];memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){dis[i]=g[1][i];}dis[1]=0;vis[1]=1;for(v=1;v<n;v++){min=mx;k=1;for(i=1;i<=n;i++){if(!vis[i]&&dis[i]<min){min=dis[i];k=i;}}vis[k]=1;if(min==mx){printf("-1\n");return ;}sum+=min;for(i=1;i<=n;i++){if(!vis[i]&&dis[i]>g[k][i])dis[i]=g[k][i];}}printf("%d\n",sum);}int main(){int T,d,e;scanf("%d",&T);while(T--){int i,j,l,x,y,k,s;scanf("%d%d%d",&n,&m,&t);for(d=1;d<=n;d++){for(e=1;e<=n;e++){if(d==e)g[d][e]=0;elseg[d][e]=mx;}}while(m--){scanf("%d%d%d",&i,&j,&l);if(g[i][j]>l)g[i][j]=g[j][i]=l;}while(t--){scanf("%d",&s);for(i=0;i<s;i++)scanf("%d",&a[i]);for(i=0;i<s;i++){for(j=1+i;j<s;j++){g[a[i]][a[j]]=g[a[j]][a[i]]=0;}}}prim();}return 0;}//法二稍作变动,简单了一点点 /*prim算法*/#include<stdio.h>#include<string.h>#define mx 0x3f3f3fint g[510][510],a[510];int n,m,t;void prim(){int v,i,j,k,min,sum=0;int dis[510],vis[510];memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){dis[i]=g[1][i];}dis[1]=0;vis[1]=1;for(v=1;v<n;v++){min=mx;k=1;for(i=1;i<=n;i++){if(!vis[i]&&dis[i]<min){min=dis[i];k=i;}}vis[k]=1;if(min==mx){printf("-1\n");return ;}sum+=min;for(i=1;i<=n;i++){if(!vis[i]&&dis[i]>g[k][i])dis[i]=g[k][i];}}printf("%d\n",sum);}int main(){int T,d,e;scanf("%d",&T);while(T--){int i,j,l,x,y,k,s;scanf("%d%d%d",&n,&m,&t);for(d=1;d<=n;d++){for(e=1;e<=n;e++){if(d==e)g[d][e]=0;elseg[d][e]=mx;}}while(m--){scanf("%d%d%d",&i,&j,&l);if(g[i][j]>l)g[i][j]=g[j][i]=l;}while(t--){scanf("%d",&s);scanf("%d",&x);for(i=1;i<s;i++){scanf("%d",&y);g[x][y]=g[y][x]=0;}}prim();}return 0;}
0 0
- Connect the Cities 3371
- hdu 3371 Connect the Cities
- Hdu-3371 Connect the Cities
- HDU 3371 Connect the Cities
- HDOJ 3371 Connect the Cities
- HDU-3371-Connect the Cities
- hdu 3371 Connect the Cities
- HDU 3371 Connect the Cities
- Hdu 3371 - Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- HDU 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- hdu 3371 Connect the Cities
- NYOJ 38 布线问题(prim算法)
- HD1301 Jungle Roads 【Kruska】
- 作业
- C++ 对象指针
- apk 签名
- Connect the Cities 3371
- 使命栈(stack)实现一个简易的四则运算计算器
- hdu 2084 数塔
- 记录一些第三方
- MCS 最小生成树#2 Kruskal
- service sshd restart sshd unrecognized service
- 黑马程序员——一维数组指针探究
- 卸载mysql5.1后再次安装不成功的解决方法
- zoj1372Networking(模板题)