码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2253(3种最短路搞法)

时间:2014-08-25 17:06:04      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   ar   

T - Frogger
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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 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

题意:一只青蛙在湖中一颗石头上, 它想去有另一只青蛙的石头上,但是 湖里的水很脏,它不愿意游泳,所以它要跳过去;

给出 两只青蛙所在石头的坐标, 及 湖里其他石头的坐标;任一两个坐标点间都是双向连通的。显然从1到2存在至少一条的通路,每一条通路的元素都是这条通路中前后两个点的距离,这些距离中又有一个最大距离。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
using namespace std;
#define INF 1e7
#define maxn 205
typedef long long LL;
int n,x[maxn],y[maxn];
double w[maxn][maxn];
double d[maxn];
bool inq[maxn];
double dist(int x1,int y1,int x2,int y2){return sqrt((double)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
struct Heapnode
{
    int u;
    double w;
    bool operator < (const Heapnode & pp) const
    {
        return w>pp.w;
    }
};
void dijkstra(double m)
{
    for(int i=0;i<=n;i++)d[i]=INF;
    d[1]=0;
    memset(inq,0,sizeof(inq));
    priority_queue<Heapnode>q;
    q.push((Heapnode){1,0});
    while(!q.empty())
    {
        Heapnode tmp=q.top();q.pop();
        int u=tmp.u;
        if(inq[u])continue;
        inq[u]=true;
        for(int i=1;i<=n;i++)if(w[u][i]<=m&&d[i]>d[u]+m-w[u][i])
        {
            d[i]=d[u]+m-w[u][i];
            if(d[2]<INF)return ;
            q.push((Heapnode){i,d[i]});
        }
    }
}
int main()
{
    int cas=0;
    while(~scanf("%d",&n)&&n)
    {
        cas++;
        for(int i=1;i<=n;i++)
            scanf("%d%d",x+i,y+i);
        for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
        {
            w[i][j]=w[j][i]=(i==j?0:dist(x[i],y[i],x[j],y[j]));
        }
        printf("Scenario #%d\n",cas);
        double l=0,r=40000.0;
        while(r-l>=0.0001)
        {
            double m=(l+r)/2;
            dijkstra(m);
            if(d[2]<INF)r=m;
            else l=m;
        }
        printf("Frog Distance = %.3f\n\n",l);
    }
    return 0;
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
using namespace std;
#define INF 1e7
#define maxn 205
typedef long long LL;
int n,x[maxn],y[maxn];
double w[maxn][maxn];
double d[maxn];
bool inq[maxn];
double dist(int x1,int y1,int x2,int y2){return sqrt((double)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
void spfa(double s)
{
    for(int i=0;i<=n;i++)d[i]=(i==1?0:INF);
    memset(inq,0,sizeof(inq));
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        inq[u]=false;
        for(int i=1;i<=n;i++)
        {
            if(w[u][i]>s)continue;
            if(d[u]-w[u][i]+s<=d[i])d[i]=d[u]+s-w[u][i];
            else continue;
            if(d[2]<INF)return ;
            if(!inq[i])
            {
                inq[i]=true;
                q.push(i);
            }
        }
    }
}
int main()
{
    int cas=0;
    while(~scanf("%d",&n)&&n)
    {
        cas++;
        for(int i=1;i<=n;i++)
            scanf("%d%d",x+i,y+i);
        for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
        {
            w[i][j]=w[j][i]=(i==j?0:dist(x[i],y[i],x[j],y[j]));
        }
        printf("Scenario #%d\n",cas);
        double l=0,r=40000.0;
        while(r-l>=0.0001)
        {
            double m=(l+r)/2;
            spfa(m);
            if(d[2]<INF)r=m;
            else l=m;
        }
        printf("Frog Distance = %.3f\n\n",l);
    }
    return 0;
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<cstring>
using namespace std;
#define INF 1e7
#define maxn 205
typedef long long LL;
int n,x[maxn],y[maxn];
double w[maxn][maxn];
double d[maxn];
bool inq[maxn];
double dist(int x1,int y1,int x2,int y2){return sqrt((double)(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
void floyd()
{
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n-1;i++)
    for(int j=i+1;j<=n;j++)
    {
        if(w[i][j]>w[i][k]&&w[i][j]>w[k][j])
        {
            if(w[i][k]>w[k][j])w[i][j]=w[j][i]=w[i][k];
            else w[i][j]=w[j][i]=w[k][j];
        }
    }
}
int main()
{
    int cas=0;
    while(~scanf("%d",&n)&&n)
    {
        cas++;
        for(int i=1;i<=n;i++)
            scanf("%d%d",x+i,y+i);
        for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
        {
            w[i][j]=w[j][i]=(i==j?0:dist(x[i],y[i],x[j],y[j]));
        }
        printf("Scenario #%d\n",cas);
        floyd();
        printf("Frog Distance = %.3f\n\n",w[1][2]);
    }
    return 0;
}




poj 2253(3种最短路搞法)

标签:des   style   http   color   os   io   strong   for   ar   

原文地址:http://blog.csdn.net/acvcla/article/details/38821595

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!