hdu 2122 Ice_cream’s world III(最小生成树))
来源:互联网 发布:windows 设置字符集 编辑:程序博客网 时间:2024/05/16 10:59
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 10 1 104 0
Sample Output
10impossible
就是给你城市的数量,和可以修建的铁路及其长度,如果连通,输出最小的总长,否则输出impossible。
#include <cstdio>#include <iostream>using namespace std;const int Max=10000000;int n,m;int map[1005][1005];int ans;int Prim(int v0){ int sum=0; int lowcost[1005]; for (int i=0; i<n; i++) { lowcost[i]=map[v0][i]; } lowcost[v0]=-1; for (int i=0; i<n-1; i++) { int min=Max; int k=-1; for (int j=0; j<n; j++) { if(lowcost[j]!=-1&&min>lowcost[j]) { min=lowcost[j]; k=j; } } if(k==-1) { break; } ans++; sum+=lowcost[k]; lowcost[k]=-1; for (int j=0; j<n; j++) { if(lowcost[j]>map[k][j]) { lowcost[j]=map[k][j]; } } } return sum;}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { ans=0; 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; } } } int a,b,c; for (int i=0; i<m; i++) { scanf("%d%d%d",&a,&b,&c); if(map[a][b]>c) //判断重边取小 { map[a][b]=c; map[b][a]=c; } } int sum=Prim(0); if(ans==n-1) { cout<<sum<<endl<<endl; } else { cout<<"impossible"<<endl<<endl; } } return 0;}
可以开始DFS看图是否联通。
bool visit[1005];void DFS(int v0){ visit[v0]=true; ans++; for (int i=0; i<n; i++) { if(!visit[i] && map[v0][i]!=Max) { DFS(i); } }}
(2)Kruscal
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int n,m;int ans;int sum;int father[1005];struct node{ int a,b; int d; bool operator<(const node &t)const { return d<t.d; }};int Find(int x){ if(x!=father[x]) { father[x]=Find(father[x]); } return father[x];}void Union(int x,int y,int d){ x=Find(x); y=Find(y); if(x!=y) { father[x]=y; ans++; sum+=d; }}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { ans=0; node num[10005]; sum=0; for (int i=0; i<n; i++) { father[i]=i; } for (int i=0; i<m; i++) { scanf("%d%d%d",&num[i].a,&num[i].b,&num[i].d); } sort(num, num+m); //因为有重边 for (int i=0; i<m; i++) { Union(num[i].a, num[i].b,num[i].d); } if(ans==n-1) { cout<<sum<<endl<<endl; } else { cout<<"impossible"<<endl<<endl; } } return 0;}
0 0
- hdu 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 最小生成树prim
- hdu 2122 Ice_cream’s world III(最小生成树))
- hdu 2122(Ice_cream’s world III)(最小生成树,两种算法都可以)
- HDU 2122 Ice_cream’s world III(最小生成树Kruskal)
- 文章标题 HDU 2122: Ice_cream’s world III (最小生成树+kruskal)
- Ice_cream’s world III【最小生成树】
- HDOJ题目2122Ice_cream’s world III(最小生成树)
- 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 【最小生成树】
- 杭电 最小生成树 2122 Ice_cream’s world III
- 最小生成树 Prim 算法HDU 2122 Ice_cream’s world III
- hdu 2122 Ice_cream’s world III 最小生成树kruskal+并查集
- 读书札记-2013年12月
- 2、搜索引擎的使用
- DSP6000的几个简单优化技巧
- 数字信号处理的学习资源
- 白话压缩感知(含Matlab代码)
- hdu 2122 Ice_cream’s world III(最小生成树))
- 如何区分javascript设计模式中的中介者模式(Mediator Pattern)与观察者模式(Observer Pattern)?
- 快速排序 算法摘记
- 如何重定向
- 如何让VM虚拟机全屏
- Oracle 中的JOIN
- 数据结构 第一章 学习感受与总结
- 深入理解嵌入式开发之系统开发篇全覆盖(MC9S12单片机、CAN技术介绍)
- JavaScript - Array对象的使用 及 数组排序 sort