Frogger

来源:互联网 发布:许晴 王雪冰 知乎 编辑:程序博客网 时间:2024/06/08 15:17

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

题意为求在所有能使得两只青蛙相遇的路径中最长边最短的,并输出那个值

解法一:

最短路

max(dis[Q.des],edge[i].w)
找到这条路径的最长边

min(dis[to],max(dis[Q.des],edge[i].w))
求出所有路径最长边最短的

#include<stdio.h>#include<string.h>#include<math.h>#include<queue>#include<algorithm>#define inf 1e8#define maxn 205#define pow2(x)  ((x)*(x))using namespace std;int cnt,head[maxn],n;bool vis[maxn];double dis[maxn];struct Edge{int v,next;double w;}edge[2*maxn*maxn];typedef struct list{int des;double weight;list() {}list(int _d,double _w){des=_d;weight=_w;}friend bool operator < (struct list a,struct list b){return a.weight>b.weight;}}list;void add_edge(int u,int v,double w){edge[cnt].v=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;}void dijkstra(int s){memset(vis,0,sizeof(vis));fill(dis,dis+n,inf);priority_queue<list> q;dis[s]=0;q.push(list(s,0));while(!q.empty()){list Q=q.top();q.pop();if(vis[Q.des])continue;vis[Q.des]=true;for(int i=head[Q.des];i!=-1;i=edge[i].next){int to=edge[i].v;dis[to]=min(dis[to],max(dis[Q.des],edge[i].w));q.push(list(to,dis[to]));}}}int main(){int t=0;while(scanf("%d",&n),n){double pos[maxn][2];cnt=0;memset(head,-1,sizeof(head));memset(edge,0,sizeof(edge));for(int i=0;i<n;i++){scanf("%lf%lf",&pos[i][0],&pos[i][1]);}for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){double w=sqrt(pow2(pos[i][0]-pos[j][0])+pow2(pos[i][1]-pos[j][1]));add_edge(i,j,w);add_edge(j,i,w);}}dijkstra(0);printf("Scenario #%d\nFrog Distance = %.3f\n\n",++t,dis[1]);}return 0;}
解法二:

最小生成树

因为在求最小生成树的过程中,所有的两点之间的距离是已经排序过的,所以只要起点和终点在同一个并查集中时,此时这条路径的最长边即为所求

                         if(fa!=fb){pre[fa]=fb;if(find(0)==find(1)){printf("Scenario #%d\nFrog Distance = %.3f\n\n",++t,v[i].w);break;}}
可以证明,当某条边加入集合中时,在它之前加入集合中的边都是比它小的

#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<vector>#define inf 1e8#define maxn 205#define pow2(x)  ((x)*(x))using namespace std;int pre[maxn];typedef struct list{int u,v;double w;list() {}list(int _u,int _v,double _w){u=_u;v=_v;w=_w;}}list;bool cmp(list a,list b){return a.w<b.w;}int find(int a){return a==pre[a]?a:pre[a]=find(pre[a]);}int main(){int n,t=0;while(scanf("%d",&n),n){double pos[maxn][2];vector<list> v;for(int i=0;i<n;i++){scanf("%lf%lf",&pos[i][0],&pos[i][1]);}for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){double w=sqrt(pow2(pos[i][0]-pos[j][0])+pow2(pos[i][1]-pos[j][1]));v.push_back(list(i,j,w));}}sort(v.begin(),v.end(),cmp);for(int i=0;i<=n;i++)pre[i]=i;for(int i=0;i<v.size();i++){int fa,fb;fa=find(v[i].u);fb=find(v[i].v);if(fa!=fb){pre[fa]=fb;if(find(0)==find(1)){printf("Scenario #%d\nFrog Distance = %.3f\n\n",++t,v[i].w);break;}}}}return 0;}

原创粉丝点击