|洛谷|图论生成树|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;
}
简单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;
}
相关文章推荐
- |洛谷|图论生成树|P1111 修复公路
- P1396 营救 洛谷
- 洛谷 P1396 营救
- 洛谷 P1396 营救
- 营救(洛谷 P1396)
- 洛谷[P1396]营救 并查集
- 洛谷 P1396 营救
- 洛谷 P1396 营救
- 图论-最小生成树
- POJ 1789 Truck History 图论 prim算法 最小生成树
- POJ 1258 Agri-Net 图论 prim算法 最小生成树
- 图论--最小生成树总结(Prim&&Kruskal)
- hdu1162 Eddy's picture(图论:最小生成树-minimal spanning tree)
- 图论-最小生成树-Prim
- poj 2349 Arctic Network(图论:最小生成树)
- (复习)图论--最小生成树--Kruskal算法
- ACM: 简单最小生成树 图论题 poj 1…
- POJ2349 Arctic Network 题解【最小生成树】【Kruskal】【图论】
- 洛谷P1462-通往奥格玛瑞的路【日常图论,二分查找,最短路,SPAF】
- 洛谷 1262 间谍网络 Tarjan 图论