UVA 534 Frogger 【最小瓶颈树】
原创
©著作权归作者所有:来自51CTO博客作者mb640163eb338d0的原创作品,请联系作者获取转载授权,否则将追究法律责任
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=475
题意:求点1到2所有路径上最大边的最小值。
解法:Kruskal按边值排序。直到1,2联通。
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <math.h>
using namespace std;
const int MAXN = 410000;//点
const int MAXM = 410000;//边
struct Edge
{
int u, v;
double w;
}edge[MAXM];
bool cmp(Edge a, Edge b)
{
return a.w < b.w;
}
int tol;
void addedge(int u, int v, double w)
{
edge[tol].u = u;
edge[tol].v = v;
edge[tol++].w = w;
}
int f[MAXN];//并查集使用
int find(int x)
{
if (f[x] == -1) return x;
else return f[x] = find(f[x]);
}
struct node
{
int x, y;
}pos[MAXN];
double get_dis(node a, node b)
{
int x = a.x - b.x;
int y = a.y - b.y;
return sqrt(x*x + y*y);
}
int main()
{
int cases = 1;
int n;
while (~scanf("%d", &n) && n)
{
tol = 0;
memset(f, -1, sizeof(-1));
for (int i = 0; i < n; i++)
{
scanf("%d %d", &pos[i].x, &pos[i].y);
for (int j = 0; j < i; j++)
addedge(i, j, get_dis(pos[i], pos[j]));
}
sort(edge, edge + tol, cmp);
for (int i = 0; i < tol; i++)
{
int u = edge[i].u;
int v = edge[i].v;
int t1 = find(u);
int t2 = find(v);
if (t1 != t2) f[t1] = t2;
if (find(1) == find(0))
{
printf("Scenario #%d\nFrog Distance = %.3lf\n\n", cases++, edge[i].w);
break;
}
}
}
return 0;
}