最小瓶颈路 Uva 534 Frogger

说明:关于Uva的题目,可以在vjudge上做的,不用到Uva(那个极其慢的)网站去做。

最小瓶颈路:找uv的一条路径满足最大边权值尽量小

先求最小生成树,然后uv的路径在树上是唯一的,答案就是这条路径。

Uva 534 Frogger

Time Limit: 3000MS 64bit IO Format: %lld & %llu

Description

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是从小到大依次连边的。)一旦明白了让这条性质这题就变得简单多了。

 1 #include<iostream>
 2 using namespace std;
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstdlib>
 6 #include<algorithm>
 7 #define  N 205
 8 struct Edge{
 9     int u,v;
10     double w;
11 }edge[N*N];
12 int n,t;
13 struct D{
14     double x,y;
15 }dian[N];
16 int father[N];
17 void add_edge(int a,int b)
18 {
19     ++t;
20     edge[t].u=a;
21     edge[t].v=b;
22     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));
23 }
24 bool cmp(Edge P,Edge Q)
25 {
26     return P.w<Q.w;
27 }
28 int find(int x)
29 {
30     return (father[x]==x?x:father[x]=find(father[x]));
31 }
32 void kruskal(double &ans)
33 {
34     for(int i=1;i<=n;++i)
35       father[i]=i;
36     sort(edge+1,edge+t+1,cmp);
37     for(int l=1;l<=t;++l)
38     {
39         int f1=find(edge[l].u);
40         int f2=find(edge[l].v);
41         if(f1==f2) continue;
42         father[f2]=f1;
43         if(find(1)==find(2)) /*1是起点,2是终点,在kruskal算法执行的时候第一次将两个点(或者说两个点的集合)连起来的那条边就是这两点的最小瓶颈路上最大边。*/
44         {
45             ans=edge[l].w;/*这个性质由kruskal算法的过程即可知道*/
46             return;
47         }
48     }
49 }
50 int main()
51 {
52     int topt=0;
53     while(scanf("%d",&n)==1)
54   {
55       topt++;
56       if(n==0) break;
57     t=0;
58     for(int i=1;i<=n;++i)
59     {
60         scanf("%lf%lf",&dian[i].x,&dian[i].y);
61         if(i==1)continue;
62         for(int j=1;j<i;++j)
63           add_edge(j,i);
64     }
65     double ans;
66     kruskal(ans);
67     printf("Scenario #%d
",topt);
68     printf("Frog Distance = %0.3lf

",ans);
69   }
70     return 0;
71 }
原文地址:https://www.cnblogs.com/c1299401227/p/5808439.html