POJ2253 Frogger

来源:互联网 发布:网络签约写手的收入 编辑:程序博客网 时间:2024/06/05 22:12
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 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
20 03 4317 419 418 50
Sample Output
Scenario #1Frog Distance = 5.000Scenario #2Frog Distance = 1.414


题意概括:给n个坐标,第一个是青蛙的起始位置第二个是终点,其它的代表青蛙中途可能经过的位置,青蛙希望每次跳的距离尽量的近,一共跳多远,跳多少次都可以。只要每次跳的距离近。所有可能路径中权值最大的段代表该路径,最后找到权值最小的段。

解题思路:先把题目给的坐标进行处理算出每两个坐标的距离,然后把距离当作权值从小到大排序,用Kruskal,直到起始点和终点属于同一个“父亲”,因为权值是从小到大排序的,第一条把起始位置和终点连接的路径就是符合题意的路径,里面的最大权值就是结果。

代码:

#include<stdio.h>#include<algorithm>using namespace std;#include<string.h>#include<math.h>struct edge {int u,v;double w;}e[1200];bool cmp(edge a,edge b){return a.w<b.w;}int m,n,l,z=0;int p[1200];double a[1200][1200],b[1200],d[1200],s[1200][5],max=1e9;void Uset(){for(int i=1;i<=n;i++){p[i]=-1;}}int find(int x){int s;for(s=x;p[s]>=0;s=p[s]);while(s!=x){int t=p[x];p[x]=s;x=t;}return s;}void Union(int u ,int v){int t1=find(u);int t2=find(v);if(t1==t2)return ;int tp=p[t1]+p[t2];if(p[t1]>p[t2]){p[t1]=t2;p[t2]=tp;}else{p[t2]=t1;p[t1]=tp;}}double min(double a,double b){if(a<b)a=b;return a;}void Kruskal(){double sum=-1;Uset();for(int i=0;i<l;i++){if(find(e[i].u)!=find(e[i].v)){Union(e[i].u,e[i].v);//sum=min(e[i].w,sum);sum=e[i].w;//printf("%d %d\n",find(1),find(2));}if(find(1)==find(2))//if(p[2]==1)break;}z++;printf("Scenario #%d\n",z);printf("Frog Distance = %.3lf\n\n",sum);}int main(){int i,j,k;while(scanf("%d",&n),n!=0){for(i=1;i<=n;i++){scanf("%lf%lf",&s[i][1],&s[i][2]);}for(i=1;i<=n;i++){for(j=1;j<=n;j++){a[i][j]=sqrt((s[i][1]-s[j][1])*(s[i][1]-s[j][1])+(s[i][2]-s[j][2])*(s[i][2]-s[j][2]));}}l=0;for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(i!=j){e[l].u=i;e[l].v=j;e[l].w=a[i][j];l++;}//printf("%.2lf    ",a[i][j]);}//printf("\n");}sort(e,e+l,cmp);/*for(i=0;i<l;i++){printf("%d %d %.2lf\n",e[i].u,e[i].v,e[i].w);}*/Kruskal();}return 0;}


原创粉丝点击