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

HDU1245 Saving James Bond(最短路+简单几何)

时间:2015-03-29 16:39:22      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:图论   spfa   

Saving James Bond

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2405    Accepted Submission(s): 454


Problem Description
This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world‘s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100×100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether he could escape.If he could,tell him the shortest length he has to jump and the min-steps he has to jump for shortest length.
 

Input
The input consists of several test cases. Each case starts with a line containing n <= 100, the number of crocodiles, and d > 0, the distance that James could jump. Then one line follows for each crocodile, containing the (x, y) location of the crocodile. Note that x and y are both integers, and no two crocodiles are staying at the same position.
 

Output
For each test case, if James can escape, output in one line the shortest length he has to jump and the min-steps he has to jump for shortest length. If it is impossible for James to escape that way, simply ouput "can‘t be saved".
 

Sample Input
4 10 17 0 27 0 37 0 45 0 1 10 20 30
 

Sample Output
42.50 5 can‘t be saved
题意:有个100*100的正方形池溏,中心以坐标(0,0)表示的二维坐标,中心有一块土地以(0,0)为中心的圆,直径为15,池溏有n条鳄鱼,坐标位置给出。现在有一个人在池中心的土地上,要逃到岸上去,可以踩着鳄鱼过去,最远只能跳 d 米,问最短的路且最少跳了几下。如果不能到达陆地,则输出can‘t be saved
分析:n条鲤鱼看成点,人的起始一个点,还有一个就是目的地(陆地)是一个点。共n+2个点;
#include<stdio.h>
#include<math.h>
#include<queue>
using namespace std;

const int N = 105;
const double inf = 0x3ffffffff;

int n,step[N],x[N],y[N];
double dis[N],mapt[N][N],d;
int ABS(int a)
{
    return a<0?-a:a;
}
void init()
{
    for(int i=1;i<n;i++)
    {
        mapt[0][i]=mapt[i][0]=sqrt(x[i]*x[i]+y[i]*y[i]*1.0)-7.5;
        if(mapt[0][i]>d)
        mapt[0][i]=mapt[i][0]=inf;

        double tx=ABS(x[i]),ty=ABS(y[i]);
        if(tx<ty)tx=ty;
        if(50.0-tx<=d)
            mapt[n][i]=mapt[i][n]=50.0-tx;
        else
            mapt[n][i]=mapt[i][n]=inf;
    }
    if(d>=50-7.5)
        mapt[0][n]=mapt[n][0]=50-7.5;
    else
        mapt[0][n]=mapt[n][0]=inf;

    for(int i=1;i<n;i++)
    for(int j=1;j<n;j++)
    {
        mapt[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        if(mapt[i][j]>d)
            mapt[i][j]=inf;
    }
    for(int i=0;i<=n;i++)
        dis[i]=inf,step[i]=99999999;
}
void spfa()
{
    int inq[N]={0},s;
    queue<int>q;

    dis[0]=0; step[0]=0;
    q.push(0);
    while(!q.empty())
    {
        s=q.front(); q.pop();
        inq[s]=0;

        for(int i=1;i<=n;i++)
        if(mapt[s][i]!=inf)
        {
            if(dis[i]>dis[s]+mapt[s][i])
            {
                step[i]=step[s]+1;
                dis[i]=dis[s]+mapt[s][i];
                if(inq[i]==0)
                        q.push(i),inq[i]=1;
            }
            else if(dis[i]==dis[s]+mapt[s][i]&&step[i]>step[s]+1)
            {
                step[i]=step[s]+1;
                if(inq[i]==0)
                        q.push(i),inq[i]=1;
            }
        }
    }
}
int main()
{
    while(scanf("%d%lf",&n,&d)>0)
    {
        x[0]=y[0]=0; n++;
        for(int i=1;i<n;i++)
            scanf("%d%d",&x[i],&y[i]);
        init();
        spfa();
        if(dis[n]==inf)
            printf("can't be saved\n");
        else
            printf("%.2lf %d\n",dis[n],step[n]);
    }
}



HDU1245 Saving James Bond(最短路+简单几何)

标签:图论   spfa   

原文地址:http://blog.csdn.net/u010372095/article/details/44727135

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