hdu 2122 Ice_cream’s world III
来源:互联网 发布:php erp 编辑:程序博客网 时间:2024/05/22 03:15
注意考虑只有一个城市的情况!!!还有题目有点问题,应该是接下来一共m行。
用Kruskal写的。
#include <iostream>#include<stdio.h>#include<algorithm>using namespace std;typedef struct{ int from,to,c;}road;road r[10000010];int fa[1010];int count;int cmp(const void *a,const void *b){ return (((road *)a)->c > ((road *)b)->c)?1:-1;}int find(int a ){ return fa[a]==a?a:fa[a]=find(fa[a]);}int main(){ int ans,n,m,i,j; while( scanf("%d %d",&n,&m)!=EOF){ int sum=0; int ok=0; ans=0; for(i=0;i<n;i++) fa[i]=i; for(i=0;i<m;i++){ int temp_1,temp_2,c; scanf("%d %d %d",&temp_1,&temp_2,&c); r[sum].from=temp_1; r[sum].to=temp_2; r[sum++].c=c; r[sum].from=temp_2; r[sum].to=temp_1; r[sum++].c=c; } qsort(r,sum,sizeof(road),cmp); if(n==1)ok=1; //只有一个城市 else for(i=0;i<sum;i++){ int fa1=find(r[i].from); int fa2=find(r[i].to); if(fa1!=fa2){ fa[fa2]=fa1; n--; ans+=r[i].c; if(n==1){ ok=1; break;} } } if(ok==1) printf("%d\n\n",ans); else printf("impossible\n\n"); } return 0;}
- [MST]hdu 2122 Ice_cream’s world III
- hdu 2122 Ice_cream’s world III
- HDU 2122Ice_cream’s world III
- hdu 2122 Ice_cream’s world III
- HDU 2122 Ice_cream’s world III
- hdu 2122:Ice_cream’s world III
- hdu 2122 Ice_cream’s world III
- 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 【最小生成树】
- hdu 2122 Ice_cream’s world III(最小生成树)
- hdu 2122 Ice_cream’s world III 最小生成树prim
- Ice_cream’s world III
- Ice_cream’s world III
- Hud 2122[Ice_cream’s world III Kruscal]
- hdoj-2122 Ice_cream’s world III
- Android WebView常见问题及解决方案汇总
- hdu 2680 Choose the best route
- hdu 3790 最短路径问题
- poj 1321 棋盘问题
- poj 2485 Highways
- hdu 2122 Ice_cream’s world III
- Vector C++ 详细用法
- hdu 1865 畅通工程再续
- 电能无线传输技术再现曙光
- 问题集1
- 如何反编译APK
- hdu 2084 数塔
- hdu 2094 产生冠军
- hdu 2647 Reward