hdoj 2122 Ice_cream’s world III 【最小生成树】
2015-03-17 22:34
316 查看
Ice_cream’s world III
[b]Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1074 Accepted Submission(s): 345
[/b]
Problem Description
ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every
city to the capital. The project’s cost should be as less as better.
Input
Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
Output
If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
Sample Input
2 1 0 1 10 4 0
Sample Output
10 impossible
kruskal+prime:
kruskal:
#include<stdio.h> #include<string.h> #include<algorithm> #define max 10000+10 using namespace std; int set[1010]; int city,road; struct line { int start; int end; int money; }num[max]; bool cmp(line a,line b) { return a.money<b.money; } int find(int p) { int child=p; int t; while(p!=set[p]) p=set[p]; while(child!=p) { t=set[child]; set[child]=p; child=t; } return p; } void merge(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) set[fx]=fy; } int main() { int i,j,x,y,c,t,need,exist; while(scanf("%d%d",&city,&road)!=EOF) { for(i=0;i<city;i++) set[i]=i; t=0; while(road--) { scanf("%d%d%d",&x,&y,&c); num[t].start=x; num[t].end=y; num[t].money=c; t++; } sort(num,num+t,cmp); need=0; for(i=0;i<t;i++) { if(find(num[i].start)!=find(num[i].end)) { merge(num[i].start,num[i].end); need+=num[i].money; } } exist=0; for(i=0;i<city;i++) { if(set[i]==i) { exist++; if(exist>1) break; } } if(exist>1) printf("impossible\n\n"); else printf("%d\n\n",need); } return 0; }
prime:
#include<stdio.h> #include<string.h> #define max 1000+10 #define INF 0x3f3f3f int visit[max],map[max][max],low[max]; int city,road; void prime() { int i,j,next; int min,mincost=0; for(i=0;i<city;i++) { visit[i]=0; low[i]=map[1][i]; } visit[1]=1; for(i=1;i<city;i++) { min=INF; next=1; for(j=0;j<city;j++) { if(!visit[j]&&min>low[j]) { min=low[j]; next=j; } } if(min==INF)//遍历全部无法连通 { printf("impossible\n\n"); return ; } mincost+=min; visit[next]=1; for(j=0;j<city;j++) { if(!visit[j]&&map[next][j]<low[j]) { low[j]=map[next][j]; } } } printf("%d\n\n",mincost); } int main() { int i,j,x,y,c; while(scanf("%d%d",&city,&road)!=EOF) { for(i=0;i<city;i++) { for(j=0;j<city;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } while(road--) { scanf("%d%d%d",&x,&y,&c); if(map[x][y]>c) map[x][y]=map[y][x]=c; } prime(); } return 0; }
相关文章推荐
- HDOJ 2122 Ice_cream’s world III(最小生成树--prime 水)
- hdoj 2122 Ice_cream’s world III 【最小生成树】
- hdoj 2122 Ice_cream’s world III【最小生成树】
- hdoj 2122 Ice_cream’s world III【最小生成树】
- hdoj 2122Ice_cream’s world III 【最小生成树 kruskal && prim】
- HDU2122--Ice_cream’s world III--最小生成树--kruskal
- hdu 2122(Ice_cream’s world III)(最小生成树,两种算法都可以)
- 文章标题 HDU 2122: Ice_cream’s world III (最小生成树+kruskal)
- HDU 2122 Ice_cream’s world III(最小生成树)
- HDU 2122 Ice_cream’s world III(最小生成树Kruskal)
- 杭电 最小生成树 2122 Ice_cream’s world III
- hdu 2122 Ice_cream’s world III(最小生成树))
- Ice_cream’s world III hdu 2122 最小生成树
- HDU 2122 Ice_cream’s world III【最小生成树】
- HDU 2122 Ice_cream’s world III 【最小生成树】
- hdu 2122 Ice_cream’s world III(最小生成树)
- 最小生成树 Prim 算法HDU 2122 Ice_cream’s world III
- hdu 2122 Ice_cream’s world III(最小生成树)
- Ice_cream’s world III【最小生成树】
- hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)