最小瓶颈路 Uva 534 Frogger
2016-08-26 06:22
267 查看
说明:关于Uva的题目,可以在vjudge上做的,不用到Uva(那个极其慢的)网站去做。
最小瓶颈路:找u到v的一条路径满足最大边权值尽量小先求最小生成树,然后u到v的路径在树上是唯一的,答案就是这条路径。
Uva 534 Frogger
Time Limit: 3000MS 64bit IO Format: %lld & %lluDescription
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping. Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence. The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones. You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.
Input
The input file will contain one or more test cases. The first line of each test case will contain the number of stones n (2 ≤ n ≤ 200). The next n lines each contain two integers xi , yi (0 ≤ xi , yi ≤ 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n − 2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying ‘Scenario #x’ and a line saying ‘Frog Distance = y’ where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
Sample Input
2
0 0
3 4
3
17 4
19 4
18 5
0
Sample Output
Scenario #1
Frog Distance = 5.000
Scenario #2
Frog Distance = 1.414
/*-------------------------------------------------*/ 解析:
原来的想法是先求最小生成树,然后倍增求出答案。这样虽然可以但是比较麻烦。对于要求U--V最小瓶颈路的总长度,这是最快的方法。 介于这道题是单对询问路上最大边的最小值,我们可以找到更简单的做法:郭华阳在国家集训队论文里介绍了最小生成树的性质。就是在kruskal算法执行的时候第一次将两个点(或者说两个点的集合)连起来的那条边就是这两点的最小瓶颈路上最大边。(因为kruskal是从小到大依次连边的。)一旦明白了让这条性质这题就变得简单多了。
#include<iostream> using namespace std; #include<cstdio> #include<cmath> #include<cstdlib> #include<algorithm> #define N 205 struct Edge{ int u,v; double w; }edge[N*N]; int n,t; struct D{ double x,y; }dian ; int father ; void add_edge(int a,int b) { ++t; edge[t].u=a; edge[t].v=b; edge[t].w=sqrt((dian[a].x-dian[b].x)*(dian[a].x-dian[b].x)+(dian[a].y-dian[b].y)*(dian[a].y-dian[b].y)); } bool cmp(Edge P,Edge Q) { return P.w<Q.w; } int find(int x) { return (father[x]==x?x:father[x]=find(father[x])); } void kruskal(double &ans) { for(int i=1;i<=n;++i) father[i]=i; sort(edge+1,edge+t+1,cmp); for(int l=1;l<=t;++l) { int f1=find(edge[l].u); int f2=find(edge[l].v); if(f1==f2) continue; father[f2]=f1; if(find(1)==find(2)) /*1是起点,2是终点,在kruskal算法执行的时候第一次将两个点(或者说两个点的集合)连起来的那条边就是这两点的最小瓶颈路上最大边。*/ { ans=edge[l].w;/*这个性质由kruskal算法的过程即可知道*/ return; } } } int main() { int topt=0; while(scanf("%d",&n)==1) { topt++; if(n==0) break; t=0; for(int i=1;i<=n;++i) { scanf("%lf%lf",&dian[i].x,&dian[i].y); if(i==1)continue; for(int j=1;j<i;++j) add_edge(j,i); } double ans; kruskal(ans); printf("Scenario #%d\n",topt); printf("Frog Distance = %0.3lf\n\n",ans); } return 0; }
相关文章推荐
- UVa 534 Frogger (最小瓶颈路)
- Frogger UVA 534(最小瓶颈路+floyd算法)
- UVA 534 Frogger 【最小瓶颈树】
- uva 534 - Frogger(最小生成树)
- uva 534(最小瓶颈路)
- 最小瓶颈路(uva 534)
- uva 11354 bond 最小瓶颈路
- Uva 534 - Frogger
- 最小生成树变形uva534
- uva 1395 - Slim Span(最小瓶颈生成树)
- uva 11354最小生成树瓶颈路(lca算法实现)(rmq在多校二中有一道题)
- uva 10816 - Travel in Desert(最小瓶颈生成树+最短路)
- UVA 11354 Bond 邦德 (RMQ,最小瓶颈MST)
- 【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)
- UVA 11354 Bond(最小瓶颈路+倍增)
- uva 10457 - Magic Car ( 最小瓶颈树 的应用)
- UVA 534 - Frogger
- UVA 534 - Frogger(kruskal扩展)
- UVa 11354 邦德(最小瓶颈路+LCA)
- 【UVALIVE 5713】秦始皇修路(最小瓶颈路+Kruskal)