zoj 1372 (最小生成树)
来源:互联网 发布:身份证借别人开淘宝 编辑:程序博客网 时间:2024/05/20 21:45
昨天把最小生成树搞明白了,做这题就随意的多了
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>using namespace std;int father[55];int p,r,sum;typedef struct{ int x,y; int len; }edge;edge num[110];bool cmp(edge a,edge b){ return a.len<b.len; }int find(int x){ return father[x]==x?x:find(father[x]); }void Kruskal(){ int i; for(i=0;i<p;i++) father[i]=i; sort(num,num+r,cmp); int a,b; sum=0; for(i=0;i<r;i++) { a=find(num[i].x); b=find(num[i].y); if(a!=b) { father[b]=a; sum+=num[i].len; } } }int main(){ int a,b,l,i; while(scanf("%d",&p)&&p) { scanf("%d",&r); for(i=0;i<r;i++) { scanf("%d%d%d",&a,&b,&l); num[i].x=a; num[i].y=b; num[i].len=l; } Kruskal(); printf("%d\n",sum); } return 0;}
- zoj 1372 (最小生成树)
- ZOJ 1372 Networking(Prim求最小生成树)
- zoj(1586)最小生成树
- zoj 2048 Highways(最小生成树)
- ZOJ QS Network(最小生成树)
- zoj 1406 最小生成树
- ZOJ 1203 最小生成树
- zoj 1258 最小生成树
- ZOJ 1586(最小生成树)
- zoj 1372 ||poj 1287 Networking(最小生成树-Kruskal)
- ZOJ 1372 Networking 最小生成树 Kruscal && Prim
- zoj 1203 Swordfish(最小生成树))
- zoj 1542 Network(最小生成树))
- zoj 1406 Jungle Roads(最小生成树))
- zoj 1586 QS Network(最小生成树))
- zoj 2158 Truck History(最小生成树))
- zoj 1914 Arctic Network(最小生成树))
- zoj 2048 Highways(最小生成树))
- 常用小方法
- HDU 1500 Chopsticks
- 常用小方法(续)
- How to Upgrade Internet Explorer 6
- Oralce序列机制
- zoj 1372 (最小生成树)
- Win32汇编——内存管理
- 常用小方法(后续)
- Remove WolfRam AntiVirus Fraud
- Hibernate 继承类 每棵树 集成的表
- Remove Backdoor.11.Win32.xnco Trojan
- struts2重新梳理---入门
- C++5×5断想之一:C++历史上最重要的图书
- How to disable Oracle XE component which is listening on 8080?