您的位置:首页 > 其它

|洛谷|图论生成树|P1396 营救

2016-10-30 16:54 246 查看
https://www.luogu.org/problem/show?pid=1396

简单Kruskal模板题

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
struct ed
{
int x,y,v;
}edge[20005];
bool cmp(ed const &a, ed const &b)
{
return a.v<b.v;
}
int father[10005];
int find(int x)
{
if (father[x]!=x) return father[x] = find(father[x]);
else return father[x];
}
int merge(int x, int y)
{
father[x] = y;
}
int n,m,s,t;
int main()
{
scanf("%d%d%d%d", &n ,&m,&s,&t);
for (int i=1;i<=n;i++) father[i] = i;
for (int i=1;i<=m;i++)
{
scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].v);
}
sort(edge+1,edge+1+m,cmp);
int ans = 0;
for (int i=1;;i++)
{
ed c = edge[i];
int x = find(c.x);
int y = find(c.y);
if (x!=y)
{
merge(x,y);
ans = max(ans, c.v);
}
x = find(s); y = find(t);
if (x==y) break;
}
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: