HDU 1879 继续畅通工程
来源:互联网 发布:阿里域名,动态ip 编辑:程序博客网 时间:2024/04/28 17:34
http://acm.hdu.edu.cn/showproblem.php?pid=1879
克鲁斯卡尔算法求解最小生成树(MST)
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int father[110],n,m;struct node{ int from,to,cost;}edge[5010];void Init(){ for(int i=1; i<=n; i++) father[i] = i;}int Find(int x){ if(x == father[x]) return x; return Find(father[x]);}void Union(int fx, int fy){ father[fy] = fx;}bool cmp(const node &A, const node &B){ return A.cost < B.cost;}int main(){// freopen("in.txt", "r", stdin); while(scanf("%d",&n) == 1 && n){ Init(); m = n*(n-1)/2; int from,to,cost,key; for(int i=0; i<m; i++){ scanf("%d%d%d%d",&from,&to,&cost,&key); edge[i].from = from,edge[i].to = to; if(key) edge[i].cost = 0; else edge[i].cost = cost; } sort(edge, edge+m, cmp);///将边按照权值大小排序 int cnt = n, ans = 0; for(int i=0; i<m && cnt > 1; i++){///cnt = 1代表已经找到一棵最小生成树 int fx = Find(edge[i].from), fy = Find(edge[i].to); if(fx != fy){ Union(fx, fy); cnt--; ans += edge[i].cost; } } printf("%d\n",ans); } return 0;}
0 0
- HDU 1879 继续畅通工程
- 继续畅通工程 hdu 1879
- hdu 1879 继续畅通工程
- HDU 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879 继续畅通工程
- HDU 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879 继续畅通工程
- HDU 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu - 1879 - 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879 继续畅通工程
- hdu 1879(继续畅通工程)
- hdu 1879 继续畅通工程
- C++智能指针之auto_ptr
- Jenkins + Unity + Android构建基于Unity的自动编译环境
- android Notification的实例与Notification API
- Find K-th smallest in N sorted arrays
- Android Handler理解
- HDU 1879 继续畅通工程
- hdu 5024 Wang Xifeng's Little Plot 2014 ACM/ICPC Asia Regional Guangzhou Online
- MySQL binlog_format (Mixed,Statement,Row)
- 字符转整型,比系统的好用!a2iLxx
- Hackerrank Pangrams
- 程序员编程艺术第二十六章:基于给定的文档生成倒排索引(含源码下载)
- 在ubuntu虚拟机中使用samba服务器与XP主机共享文件夹
- zoj1154
- 【软考】计算机硬件组成