POJ 2253 Frogger (最短路)

【题目链接】
http://poj.org/problem?id=2253

题目意思

两只青蛙,一只想跳到另一只石子上,现在给定n个石子坐标(第一和第二为青蛙坐标)问青蛙最少跳多远才能不掉入水的情况到另一只青蛙石子上。

解题思路

用单源最短路完成,不过dis不在存储从起点到某点最短距离,而存储这条路劲中最大距离的一段。(这题输出%lf过不了要用%f坑死了,wa到o(╥﹏╥))

代码部分


#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 205;
double dis[N];   ///存储最短路 
int vis[N];  ///记录是否在队列 
int n;
double w[N][N];  ///两点间的距离 
struct poin
{
    int x,y;
}po[N];  ///点 
void init()  ///初始化 
{
    for (int i = 0;i <= n; i++)
    {
        dis[i] = inf;
        vis[i] = 0; 
    }
}
double Width(poin x,poin y) ///两点距离 
{
    double w;
    w = sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
    return w;
}
void spfa()
{
    queue<int>q;
    q.push(1);
    vis[1] = 1;
    dis[1] = 0;
    while (!q.empty())
    {
        int t = q.front();
        q.pop();
        for (int i = 1; i <= n; i++)
        {
            if (dis[i] > max(dis[t],w[t][i]))  ///松弛 
            {
                dis[i] = max(dis[t],w[t][i]); 
                if (!vis[i])   ///判断是否在队列 
                {
                    q.push(i);   
                    vis[i] = 1;
                }
            }
        }
        vis[t] = 0;
    }
}
int main()
{
    int cas = 1;
    while (scanf("%d",&n),n)
    {
        init();
        for(int i =1; i <= n;i++)
        {
            double s;
            scanf("%d %d",&po[i].x,&po[i].y);
            for (int j= 1 ;j <= i; j++)  ///处理坐标边距离 
            {
                s = Width(po[i],po[j]);
                w[i][j] = s;
                w[j][i] = s;
            }
        }
        spfa();
        printf("Scenario #%d\n",cas++);
        printf("Frog Distance = %.3f\n\n",dis[2]);
    }
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值