POJ 2485 Highways
2015-08-08 15:15
337 查看
求最小生成树的最大边 版子中的ans 的加操作改成 max即可
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #include<set> #include<stack> #define bug puts("bugbugbug"); using namespace std; typedef long long ll; const int maxm=1000005; int f[1005]; int n,m; struct node { int x, y, v; bool operator<(const node &b)const { if(this->v!=b.v) return (this->v<b.v); if(this->x!=b.x) return (this->x<b.x); return (this->y<b.y); } } e[maxm]; int find(int x) { return f[x]= f[x]==x?x:find(f[x]); } int Kruscal() { sort(e, e + m); int cnt = n, ans = 0; for(int i = 0; i <= n; i++) f[i] = i; for (int i = 0; i < m; i++) { int x = find(e[i].x); int y = find(e[i].y); if (x != y) { f[x] = y; cnt--; ans=max(ans,e[i].v); if (cnt == 1) break; } } if(cnt!=1)return -1; return ans; } int main() { int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d",&n); m=0; for(int i=0; i<n; i++) for(int j=0; j<n; j++) { int a; scanf("%d",&a); if(j>i) { e[m].x=i; e[m].y=j; e[m++].v=a; } } printf("%d\n",Kruscal()); } }
相关文章推荐
- 设计模式--桥接模式
- 投影
- IOS中UIActionSheet使用方法详解
- 这是我的第一篇日志
- springmvc 集合类型绑定
- 机房收费系统颗粒大归仓之----打印报表
- JAVA 注解的几大作用及使用方法详解
- 设计模式一览
- QT图形视图框架
- Linux操作系统-标准IO库(2)
- 按位存储
- Valid Palindrome
- 存储着色器
- 并发:Callabel 接口
- 手机开发之H5+规范的原生UI警示框
- UITextField
- dubbo总结(7)dubbo注册中心集群
- 利用HttpClient抓取话费详单等信息
- leetcode--Word Break II
- Inversion(hdu 4911)