UVa1395(最小值最小生成树+并查集)
来源:互联网 发布:淘宝卖家不给发票投诉 编辑:程序博客网 时间:2024/05/20 20:43
//思路:用结构体保存一条边的信息(u,v,w),因为是求最小的生成树,所以把边排序后,从最小的边(起点)依次枚举可以构成的生成树
AC源码:
#include <cstdio>#include <iostream>#include <algorithm>#include <vector>using namespace std;const int INF=(1<<30);int n,m,MIN_NUM=INF,p[105];int findset(int x){return p[x]==x?x:p[x]=findset(p[x]);}struct Edge{int u,v,w;Edge(int a,int b,int c):u(a),v(b),w(c){}bool operator<(const Edge& rhs){return w<rhs.w;}};vector<Edge> Evec;void solve(){int len=Evec.size();for(int i=0;i<=len-(n-1);++i){int cnt=n;for(int k=1;k<=n;++k)p[k]=k;for(int j=i;j<len;++j){int x=findset(Evec[j].u),y=findset(Evec[j].v);if(x!=y){p[x]=y;if(--cnt==1){MIN_NUM=min(MIN_NUM,Evec[j].w-Evec[i].w);break;}}}}}int main(){while(scanf("%d %d",&n,&m)&&n){Evec.clear();int u,v,w;while(m--){scanf("%d %d %d",&u,&v,&w);Evec.push_back(Edge(u,v,w));}sort(Evec.begin(),Evec.end());MIN_NUM=INF;solve();if(MIN_NUM==INF)printf("-1\n");elseprintf("%d\n",MIN_NUM);}return 0;}
阅读全文
0 0
- UVa1395(最小值最小生成树+并查集)
- UVA1395-Slim Span(最小生成树Kruskal、并查集)
- Uva1395 最小生成树
- uva1395(最小瓶颈生成树变形)
- UVa1395图论之最小生成树
- uva1395 Kruskal算法求最小生成树
- Uva1395 Slim Span(最小生成树)
- Uva1395.Slim Span(最小生成树变形)
- 并查集与最小生成树
- 并查集模版&最小生成树
- 最小生成树kruscal+并查集
- 并查集+最小生成树_HDU_1102
- bzoj1050(最小生成树 并查集)
- 最小生成树与并查集
- 最小生成树与并查集
- 最小生成树+并查集+ kruskal
- 并查集 最小生成树
- 最小生成树(并查集)
- notedup打开时提示:Java has not been found on your computer解决办法
- 147. class_exists()
- JSP中getparametervalues获得复选框选中的值
- Python的基本数据类型
- qualcomm的camera马达驱动代码分析
- UVa1395(最小值最小生成树+并查集)
- ubuntu搭建Gerrit代码审核服务器
- C++动态规划算法之子集的和
- Illegalaccessexception
- Xilinx hdenH焊板子过程错误分析
- 第一篇博客
- 超服贴!前端必修的项目练习之路
- php 常用类汇总
- 【c++语言基础】虚函数的概念