poj Frogger(Dijkstra,Floyd,Spfa)

 Frogger

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
题意:求在起点到终点的所有路径中,每条路径中的最长跳跃距离的集合中最短的(最长)跳跃距离(也就是求能跳到终点的最小跳跃距离)
(能跳到终点是前提,最小的跳跃距离是一个梗)
类似于poj Heavy Transportation,和这个相反
思路:模板题变形,变形一下d[i]即可
Dijkstra代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#define max(a,b) (a>b?a:b)

const int maxn =200+10;
const double inf=0x3f3f3f3f;
int n;
int inq[maxn];
double d[maxn],map[maxn][maxn];
double x[maxn],y[maxn];

void dijkstra(int st,int ed)
{
    int i,j,v;
    for(i=1;i<=n;i++)
    {
        inq[i]=0;
        d[i]=map[i][st];//d[i]为最长跳跃距离
    }
    inq[st]=1;
    for(i=1;i<n;i++)
    {
        double minn=inf;
        for(j=1;j<=n;j++)
            if(!inq[j]&&d[j]<minn)
            minn=d[j],v=j;
        if(minn==inf)
            break;
        inq[v]=1;
        for(j=1;j<=n;j++)
            if(!inq[j]&&d[j]>max(d[v],map[v][j]))
                d[j]=max(d[v],map[v][j]);
    }
    printf("Frog Distance = %.3lf\n",d[ed]);
}
int main()
{
    int casr=1;
    while(~scanf("%d",&n),n)
    {
        int i,j;
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(i==j)
                    map[i][j]=0;
                else
                    map[i][j]=map[j][i]=inf;

        for(i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
            for(j=1;j<i;j++)
                map[i][j]=map[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        }
        printf("Scenario #%d\n",casr++);
        dijkstra(1,2);
        puts("");
    }
    return 0;
}

Floyd代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
const int maxn=200+10;
const double inf=0x3f3f3f3f;
double map[maxn][maxn];
double x[maxn],y[maxn];
int n;
int main()
{
    int casr=1;
    while(~scanf("%d",&n),n)
    {
        int i,j,k;
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(i==j)
                    map[i][j]=0;
                else
                    map[i][j]=map[j][i]=inf;
        for(i=1; i<=n; i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
            for(j=1; j<i; j++)
                map[i][j]=map[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        }
        for(k=1; k<=n; k++)
        {
            for(i=1; i<=n; i++)
            {
                for(j=1; j<=n; j++)
                    if(map[i][j]>max(map[i][k],map[k][j]))
                        map[j][i]=map[i][j]=max(map[i][k],map[k][j]);
            }
        }
        printf("Scenario #%d\nFrog Distance = %.3lf\n",casr++,map[1][2]);
        puts("");
    }
    return 0;
}


spfa代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;

#define maxn 100020+10
#define min(a,b)  (a<b?a:b)
#define max(a,b)  (a>b?a:b)
#define mem(a,b)  memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;

struct node
{
    int v,next;
    double w;
} G[maxn];
int u[maxn],v[maxn],first[maxn];
double d[maxn];
bool inq[maxn];
int n,len;

void spfa()
{
    int i,st;

    for(i=1; i<maxn; i++)
    {
        d[i]=inf;
        inq[i]=0;
    }
    inq[1]=1,d[1]=0;
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        inq[st]=0;
        for(i=first[st]; i!=-1; i=G[i].next)
        {
            int v=G[i].v;
            double w=G[i].w;
            if(d[v]>max(d[st],w))
            {
                d[v]=max(d[st],w);
                if(!inq[v])
                {
                    inq[v]=1;
                    q.push(v);
                }
            }
        }
    }
}

void add_egde(int u,int v,double w)
{
    G[len].v=v,G[len].w=w;
    G[len].next=first[u];
    first[u]=len++;
}

int main()
{
    int t=1;
    while(~scanf("%d",&n),n)
    {
        mem(first,-1);
        double w;
        len=1;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&u[i],&v[i]);
            for(int j=1; j<i; j++)
            {
                w=sqrt((double)(u[i]-u[j])*(double)(u[i]-u[j])+(double)(v[i]-v[j])*(double)(v[i]-v[j]));
                add_egde(j,i,w);
                add_egde(i,j,w);
            }
        }
        spfa();
        printf("Scenario #%d\nFrog Distance = %.3lf\n",t++,d[2]);
        puts("");
    }
    return 0;
}


ps:开始还不知道怎么存图 敲打,原来只要按着输入顺序把每一个点存进去就ok了0.0,受益匪浅啊。还有初始化和松弛加在一起的妙处,越想越觉得佩服 生气
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值