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

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]);
    }
}
时间: 2024-10-08 11:06:25

HDU1245 Saving James Bond(最短路+简单几何)的相关文章

PTA 07-图5 Saving James Bond - Hard Version (30分)

07-图5 Saving James Bond - Hard Version   (30分) 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 lan

HDU 1245 Saving James Bond

计算几何+SPFA 我已经不想看我的提交记录了.... HDU 我起码WA了2页.... 都是浮点数惹的祸. const double eps=1e-4; abs(a-b)<=eps; 这样来判断相等. 总共 n 条鳄鱼,最多有 n*(n+1)/2 条路. 抽象化处理. 把 中心的起点当作 起点0 : 最多有 n+1 条路. 把鳄鱼和周围的边界的终点都当作 n+1 ; 最多有 n+1 条 总共就只存在 n+2个点. 就是计算0 和 n+1 的最短距离. 有个小优化,就是当 跳跃距离能够直接跳到岸

05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 capture

05-2. Saving James Bond - Easy Version (25)

05-2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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

pat05-图2. Saving James Bond - Easy Version (25)

05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 capture

PAT Saving James Bond - Easy Version

Saving James Bond - Easy Version Assume that the lake is a 100 by 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

Saving James Bond - Hard Version(PAT)

Saving James Bond - Hard Version(30 分) 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

Saving James Bond - Easy Version (MOOC)

06-图2 Saving James Bond - Easy Version (25 分) 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

06-图2 Saving James Bond - Easy Version

这题比较简单,用了数组.在007跳跃范围内,找到鳄鱼,依次直到岸边 return yes.否则No 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