POJ
来源:互联网 发布:怎么用c语言玩单片机 编辑:程序博客网 时间:2024/06/14 20:49
题目链接:http://poj.org/problem?id=1287
题意:给你点边距离求最小生成树
思路:完全的一个模板题
代码:
#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <numeric>#include <set>#include <string>#include <cctype>#include <sstream>#define INF 0x3f3f3f3fusing namespace std;typedef long long LL;typedef pair<int, int> P;const int maxn = 5e5 + 5;int n,m,p[55];struct edge{ int u,v,cost;}e[maxn];bool cmp(edge a,edge b){ return a.cost<b.cost;}void init (int n) {for (int i=1;i<=n;i++) p[i]=i;}int Find(int x) {return p[x]==x?x: ( p[x]=Find ( p[x] ) );}bool same(int a,int b) {return Find(a)==Find(b);}void unite (int x,int y){ x=Find(x); y=Find(y); if (x!=y) p[x]=y;}int kruskal(){ sort(e,e+m,cmp); init(n); int ans=0; for (int i=0;i<m;i++){ edge ee=e[i]; if (!same(ee.u,ee.v)){ unite(ee.u,ee.v); ans+=ee.cost; } } return ans;}int main () { //freopen ("in.txt", "r", stdin); while (~scanf ("%d%d",&n,&m)&&n){ memset(e,0,sizeof(e)); for (int i=0;i<m;i++){ int u,v,c; scanf ("%d%d%d",&u,&v,&c); e[i].u=u; e[i].v=v; e[i].cost=c; } printf ("%d\n",kruskal()); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Java序列化与反序列化
- bind源码解析(二)
- String和基本数据类型的比较方式
- Python图形用户界面-Tkinter
- 普元 EOS Platform 7.6 自行封装的webservice服务,报错
- POJ
- Dwarf tower
- caffe2 ubuntu16.04 安装错误 cannot find -lopencv_dep_cudart
- Gulp常用插件实践
- Junit4使用
- 如何将图片格式转换成png
- UNIX环境高级编程习题——第四章
- mybatis+oracle添加数据时如果数据存在就更新,如果不存在就插入
- 【备忘】Struts2+Spring3+Hibernate4+Maven+EasyUI整合入门视频教程