Gym
来源:互联网 发布:经营类网络餐厅游戏 编辑:程序博客网 时间:2024/05/17 08:19
Gym - 100712FTravelling Salesman(最小生成树Kruskal 水题)
- 第一次接触Kruskal:sad:,看到一篇博客写Kruskal是并查集的完美应用,关于并查集这篇博客讲的很好,转一下
- AC代码
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{ int u,v,w; bool operator <(const node&b)const { return w<b.w; }}a[110000];int f[110000];void init(int n){ for(int i=1;i<=n;i++) f[i]=i;}int find(int x){ if(f[x]==x) return x; return f[x]=find(f[x]);}void unite(int x,int y){ x=find(x),y=find(y); if(x==y) return ; f[x]=y;}int main(){ int T; scanf("%d",&T); while(T--) { int n,m,ans=0; scanf("%d%d",&n,&m); init(n); memset(a,0,sizeof(a)); for(int i=0;i<m;i++) scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w); sort(a,a+m); for(int i=0;i<m;i++) { if(find(a[i].u)!=find(a[i].v)) { ans=a[i].w; unite(a[i].u,a[i].v); } } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Android快速开发系列 10个常用工具类
- jxl(解析,写入)Excel表格~学习日记
- Mysql修改root密码的几种方法
- JSP 9大内置对象及方法
- python matplotlib 设置全局参数
- Gym
- 关于使用anaconda出现CondaHTTPError问题的解决
- 观察者模式(Observer)-----基于JAVA语言
- Jetbrain
- 网络配置:桥接 bond team
- 【数据结构作业五】以邻接表作存储结构,广度遍历图的优先搜索序列
- PHP中使用CURL模拟发送GET,POST,PUT,DELETE请求
- python中创建dataframe数据,并将其转换成举证,对矩阵进行添加行列操作
- VMWare VMNet 8 的配置使用