HDU 3371 Connect the Cities 【最小生成树,Prime算法+Kruskal算法】
来源:互联网 发布:php开源websocket 编辑:程序博客网 时间:2024/06/08 03:33
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17167 Accepted Submission(s): 4335
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
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371
题意:由于海平面上升,有一些城市消失了,有些城市间还有相连,但更多的城市间的已经不相连了,现在政府要修建一些路,使得所有的城市都相连,问你将所有城市连接起来还要花费多少钱。
输入n,m,k,分别代表城市总数,可修建的路的条数,现在任然连接的城市数。
接下来的m行各有三个数,分别代表两个城市间修路的费用,
最后k行,每行第一个数字 t 代表后面的 t 个城市仍然相连。
一看就知道是最小生成树,但有点改动,因为有一部分已经相连。再用Kruskal算法的时候把还相连的城市用并查集连在一起,而用Prime算法的把还相连的城市之间的修路的费用记为0就可以了。
Kruskal算法AC代码:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;struct node{ int s,e,w;} a[25000+5];int n,m,k;int fa[505];bool cmp(node a,node b){ return a.w<b.w;}int Find(int x){ if(fa[x]==x) return x; return fa[x]=Find(fa[x]);}int Kruskal(){ sort(a+1,a+m+1,cmp); int ans=0; for(int i=1; i<=m; i++) { int x=Find(a[i].s); int y=Find(a[i].e); if(x!=y) { ans+=a[i].w; fa[x]=y; } } return ans;}int main(){ int T; cin>>T; while(T--) { cin>>n>>m>>k; for(int i=1; i<=m; i++) { scanf("%d%d%d",&a[i].s,&a[i].e,&a[i].w); } for(int i=1; i<=n; i++) fa[i]=i; for(int i=1; i<=k; i++) { int t,x,y; scanf("%d%d",&t,&x); for(int j=2; j<=t; j++) { scanf("%d",&y); int fx=Find(x); int fy=Find(y); if(fx!=fy) fa[fx]=fy; } } int ans=Kruskal(); int sum=0; for(int i=1; i<=n; i++) { if(fa[i]==i) sum++; } if(sum>1) cout<<"-1"<<endl; else cout<<ans<<endl; } return 0;}
Prime算法AC代码:
#include <cstdio>#include <cstring>using namespace std;int a[505][505];int dis[505];bool vis[505];int t[505];const int INF=0x3f3f3f3f;int n,m,k;int Prime(){ for(int i=1; i<=n; i++) { vis[i]=false; dis[i]=a[1][i]; } vis[1]=true; dis[1]=0; int ans=0; for(int i=1; i<n; i++) { int minn=INF; int p=-1; for(int j=1; j<=n; j++) { if(!vis[j]&&dis[j]<minn) minn=dis[p=j]; } vis[p]=true; if(p==-1) return -1; ans+=minn; for(int j=1; j<=n; j++) { if(!vis[j]&&dis[j]>a[p][j]) dis[j]=a[p][j]; } } return ans;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { if(i==j) a[i][j]=0; else a[i][j]=INF; } } int x,y,z; for(int i=1; i<=m; i++) { scanf("%d%d%d",&x,&y,&z); if(z<a[x][y]) a[x][y]=a[y][x]=z; } while(k--) { scanf("%d",&x); for(int i=1; i<=x; i++) { scanf("%d",&t[i]); } for(int i=1; i<x; i++) { for(int j=i+1; j<=x; j++) a[t[i]][t[j]]=a[t[j]][t[i]]=0; } } printf("%d\n",Prime()); } return 0;}
0 0
- HDU 3371 Connect the Cities 【最小生成树,Prime算法+Kruskal算法】
- hdu 3371 Connect the Cities 最小生成树(kruskal算法)
- HDU 3371 Connect the Cities 并查集+Kruskal算法+最小生成树
- hdu 3371 Connect the Cities(最小生成树kruskal)
- HDU 3371 Connect the Cities (最小生成树+克鲁斯卡尔算法)
- hdu 3371 Connect the Cities,(kruskal算法)
- HDOJ 3371 Connect the Cities(最小生成树--prime 水)
- 文章标题 HDU 3371 : Connect the Cities(最小生成树--Kruskal+并查集)
- HDU3371 Connect the Cities 【最小生成树Kruskal】
- hdu 3371 Connect the Cities(最小生成树)
- HDU 3371 Connect the Cities(最小生成树)
- hdu 3371 最小生成树Connect the Cities
- hdu oj 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 最小生成树
- 快速排序
- Android 存储系统之架构篇
- PAT甲级练习题A1007.Maximum Subsequence Sum (25)
- 【poj1740】 A New Stone Game
- 串行口工具
- HDU 3371 Connect the Cities 【最小生成树,Prime算法+Kruskal算法】
- BZOJ 1087 [SCOI2005]互不侵犯King
- Telerik for Winform 2010版下载、安装、初次使用
- C++中几个问题
- Java解析json数据
- jade
- C++基础相关杂烩
- ZMQ指南
- Poj2960 S-Nim