hdu 3371 Connect the Cities(最小生成树))
来源:互联网 发布:成都表演 知乎 编辑:程序博客网 时间:2024/06/05 11:23
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10336 Accepted Submission(s): 2941
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
给出n个点,m条边,以及k组已经修好的路,问联通所有城市要需要至少多少的修路费。
g++过不了,c++就可以过而且还是593ms 怎么可以这样呀。。。
目前用的并查集
#include <iostream>#include <stdio.h>using namespace std;int father[505];int size[505];struct node{ int x; int y; int w;}a[25005];int Find(int x){ while (x!=father[x]) { x=father[x]; } return x;}int cmp(const void *a1,const void *b1){ node *a=(node*)a1; node *b=(node*)b1; return a->w-b->w;}int main(){ int T,t,n,m,k,ans; scanf("%d",&T); while (T--) { scanf("%d%d%d",&n,&m,&k); ans=0; for (int i=1; i<=n; i++) { father[i]=i; size[i]=i; } for (int i=0; i<m; i++) { scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w); } for (int i=0; i<k; i++) { int x,y; scanf("%d%d",&t,&x); for (int j=1; j<t; j++) { scanf("%d",&y); int fx=Find(x); int fy=Find(y); if(fx!=fy) { if(size[fx]>size[fy]) { size[fx]+=size[fy]; father[fy]=fx; } else { size[fy]+=size[fx]; father[fx]=fy; } } } } qsort(a, m, sizeof(a[0]), cmp); for (int i=0; i<m; i++) { int fx=Find(a[i].x); int fy=Find(a[i].y); if(fx!=fy) { if(size[fx]>size[fy]) { size[fx]+=size[fy]; father[fy]=fx; } else { size[fy]+=size[fx]; father[fx]=fy; } ans+=a[i].w; } } int count=0; for (int i=1; i<=n; i++) { if(father[i]==i) { count++; } } if(count==1) { printf("%d\n",ans); } else { printf("-1\n"); } } return 0;}
Prim
#include <stdio.h>#include <stdlib.h>#include <algorithm>using namespace std;int map[505][505];int num[105];const int Max=10000000;int n;void Prim(int v0){ int sum=0; int lowcost[505]; for (int i=0; i<n; i++) { lowcost[i]=map[v0][i]; } lowcost[v0]=-1; for (int i=1; i<n; i++) { int min=Max; int k=-1; for (int j=0; j<n; j++) { if( lowcost[j]!=-1 && min>lowcost[j]) { k=j; min=lowcost[j]; } } if(k==-1) { printf("-1\n"); return; } sum+=lowcost[k]; lowcost[k]=-1; for (int j=0; j<n; j++) { if(lowcost[j]!=-1 && map[k][j]<lowcost[j]) { lowcost[j]=map[k][j]; } } } printf("%d\n",sum);}int main(){ int t,m,k; scanf("%d",&t); while (t--) { scanf("%d%d%d",&n,&m,&k); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { if(i==j) { map[i][j]=0; } else { map[i][j]=Max; } } } for (int i=0; i<m; i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(map[a-1][b-1]>c) { map[a-1][b-1]=map[b-1][a-1]=c; } } for (int i=0; i<k; i++) { int p; scanf("%d",&p); for (int j=0; j<p; j++) { scanf("%d",&num[j]); } for (int j=0; j<p; j++) { for (int x=0; x<p; x++) { map[num[j]-1][num[x]-1]=0; //置为0 } } } Prim(0); } return 0;}
0 0
- hdu 3371 Connect the Cities(最小生成树))
- hdu 3371(Connect the Cities)(最小生成树)
- hdu 3371 Connect the Cities(最小生成树)
- hdu oj 3371 Connect the Cities (最小生成树)
- HDOJ 题目3371Connect the Cities(最小生成树)
- 3371 Connect the Cities(最小生成树)
- 文章标题 HDU 3371 : Connect the Cities(最小生成树--Kruskal+并查集)
- HDU 3371 Connect the Cities (最小生成树+克鲁斯卡尔算法)
- hdu 3371 Connect the Cities 最小生成树(kruskal算法)
- 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(最小生成树kruskal)
- hdu 3371 Connect the Cities 最小生成树
- hdu 3371 Connect the Cities 最小生成树prim
- HDU-3371 Connect the Cities(最小生成树)
- HDU 3371Connect the Cities(最小生成树)
- boa cgi html
- DUILIB异形窗口实现
- ACdream OJ 1020 The Game about KILL
- 黑马程序员_C#基础篇总结5
- duilib进阶教程 -- 改进List控件(3)_支持多选+右键菜单(ctrl,shift, 鼠标滑动框选)
- hdu 3371 Connect the Cities(最小生成树))
- UVA 610 - Street Directions(割边)
- JavaSe基础XX17——常用对象API-集合框架_4
- duilib进阶教程 -- 实现List排序
- Java Web应用在ARM Linux平台上的实现
- 读《考拉小巫的英语学习日记》-----作为自己IT路上的不断鼓励
- duilib进阶教程 -- 改进List控件_优化支持多选拉伸 (2)
- duilib进阶教程 -- 改进List控件 (1)
- 刷品牌关注为店铺未来的发展提供动力