poj 1287 Networking
来源:互联网 发布:淘宝联盟鹊桥入口 编辑:程序博客网 时间:2024/05/17 16:55
原题链接:http://poj.org/problem?id=1287
最小生成树kruskal的简单用法,具体实现如下:
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>#define Max_N 5000typedef struct node_t{ int from, to, cost; }edge;edge es[Max_N];int V,E,par[Max_N], rank[Max_N];void init(int n){int i;for (i = 0; i < n; i++){par[i] = i;rank[i] = 0;}}int find(int x){if (x == par[x]) return x;else return par[x] = find(par[x]);}void unite(int x, int y){x = find(x), y = find(y);if (x == y) return;if (rank[x] < rank[y]) par[x] = y;else{par[y] = x;if (rank[x] == rank[y]) rank[x]++;}}int same(int x, int y){return find(x) == find(y);}int cmp(const void *a, const *b){edge t = *(edge *)a, _t = *(edge *)b;return t.cost - _t.cost;}int kruskal(){init(V);qsort(es, E, sizeof(es[0]), cmp);int i, res = 0;for (i = 0; i<E; i++){edge e = es[i];if (!same(e.from, e.to)){unite(e.from, e.to);res += e.cost;}}return res;}int main(){#ifdef LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w+", stdout);#endifchar ch;int i,a,b,c;while (EOF != scanf("%d", &V) && 0 != V){scanf("%d", &E);for (i = 0; i < E; i++){scanf("%d %d %d", &a, &b, &c);es[i] = (edge){ --a, --b, c };}printf("%d\n", kruskal());scanf("%c", &ch);}return 0;}
0 0
- poj 1287 Networking
- POJ-1287-Networking
- poj 1287 networking
- POJ 1287 Networking
- POJ:1287 Networking
- POJ 1287 Networking
- poj 1287 Networking
- POJ-1287 Networking
- poj 1287 Networking
- POJ 1287 Networking
- poj 1287 Networking
- poj 1287 networking kruskal
- POJ 1287 Networking
- poj 1287 Networking
- POJ 1287 Networking
- POJ 1287 Networking
- Poj 1287 Networking
- POJ 1287 Networking
- sans webcast
- java 简单介绍
- findViewById()后对象仍为空
- 4 JAVA 数组的应用 金额变大写
- JVM垃圾回收总结
- poj 1287 Networking
- unity3d游戏开发经验之对于关卡类游戏的技巧
- unity3D游戏开发之浅谈如何提高游戏生命力
- EJB-MDB
- 标准的软件工程过程之文档标准
- 一个统计指定递增方式的基础向量的个数问题
- unity3f游戏开发之游戏设计中运营重用体系
- java Jstatd命令(Java Statistics Monitoring Daemon)
- unity3d游戏开发之我的游戏理念