poj 2395
来源:互联网 发布:java作业汽车租赁系统 编辑:程序博客网 时间:2024/05/17 06:18
最小生成树Kruskal算法。
#include <iostream>#include <algorithm>using namespace std;const int MAXN = 1005;const int MAXM = 20005;int n, m, a, b, l;int cost[MAXN][MAXN];int par[MAXN];int rank[MAXN];struct edge {int x;int y;int cost;}es[MAXN];bool cmp(const edge& e1, const edge& e2) {return e1.cost < e2.cost;}void init(int n) {for(int i=0; i<n; i++) {par[i] = i;rank[i] = 0;}}int find(int x) {if(par[x] == x)return x;elsereturn 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]++;}}bool same(int x, int y) {return find(x) == find(y);}int main() {scanf("%d%d", &n, &m);int ids = 0;for(int i=0; i<m; i++) {scanf("%d%d%d", &a, &b, &l);es[ids].x = a;es[ids].y = b;es[ids++].cost = l;}sort(es, es+m, cmp);init(n);int res = 0;int max = -1;for(int i=0; i<m; i++) {edge e = es[i];if(!same(e.x, e.y)) {unite(e.x, e.y);max = max > e.cost ? max : e.cost;}}printf("%d\n", max);return 0;}
0 0
- poj 2395
- POJ 2395
- poj-2395
- POJ 2395
- poj 2395
- POJ 2395
- poj 2395 Kruskal
- POJ 2395 Prim
- POJ 2395 prim
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- angularJs ng-show命令隐藏
- Android--RxJava源码初探
- 剑指offer--面试题45: 孩子们的游戏(圆圈中最后剩下的数)
- H5项目常见问题及注意事项
- Touch ID 简易教程
- poj 2395
- Java 8的新特性—终极版
- 华为oj之质数因子
- jsp里的逻辑语句c:if和c:choose
- MAMP下改网站根目录使用域名重定向
- maven搭建ssm项目(入门)
- 利用js判断浏览器类型(是否为IE,Firefox,Opera浏览器)
- <转>简单易懂的Git使用教程
- 探索 CSS3 中的 box-shadow 属性