hdu 1245 Saving James Bond【floyd】

原创 2016年05月31日 18:04:15

Saving James Bond

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

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

Author

weigang Lee

 

题目大意:在直角坐标系中,以(-50,50)和(50,-50)为两个顶点组成的矩形表示这是要逃脱的区域,有一个以原点为中心,以15为直径的原型岛屿,主人公从岛屿出发,跳出矩形,问在每次跳跃不超过距离d条件下的:最小跳出的距离,和这种条件下的最小步数。


概括思路:

设定起点为(0,0),然后求出起点到其他各个点的最短距离,然后对于map【0】【i】,枚举其点i到终点的距离,然后加上map【0】【i】求最小的即可;


细节实现:

1、因为我们从起点出发其实并不是从(0,0)点出发跳跃,而是从以其围成的圆上的点出发开始跳跃的,所以:

map【0】【i】=dis(原点,i)-7.5;
map【i】【j】=dis(i,j);

2、对于求最短路部分:jumpp【i】【j】表示点i跳到点j需要跳跃几步。

因为点比较少,所以直接floyd即可,在floyd的过程的同时,其实就可以处理掉跳跃几步的问题:

if(map【j】【k】 >map【j】【i】+map【i】【k】)map【j】【k】 =map【j】【i】+map【i】【k】;jumpp【j】【k】=jumpp【j】【i】+jumpp【i】【k】;

这里注意一个点,如果map【j】【k】 ==map【j】【i】+map【i】【k】,我们要对jumpp【】【】进行单独更新,代码实现为:

        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                for(int k=0;k<=n;k++)
                {
                    if(map[j][k]>map[j][i]+map[i][k])
                    {
                        map[j][k]=map[j][i]+map[i][k];
                        jump[j][k]=jump[j][i]+jump[i][k];
                    }
                    if(fabs(map[j][k]-map[j][i]+map[i][k])<eps)//精度控制细节
                    {
                        jump[j][k]=min(jump[j][k],jump[j][i]+jump[i][k]);
                    }
                }
            }
        }
3、这样我们就能求出从起点到其他各点的距离,然后我们枚举所有map【0】【i】,并且判断从点i到终点的距离能否跳跃过去,如果能,相对比较大小即可,代码实现为:

        for(int i=0;i<=n;i++)
        {
            double dd=min(min(50-x[i],x[i]+50),min(50-y[i],y[i]+50));//点i到终点的距离。
            if(dd>d)continue;//如果跳不到终点继续判断下一个点。
            else
            {
                if(output>map[0][i]+dd)
                {
                    output=map[0][i]+dd;
                    jumpp=jump[0][i]+1;
                    ok=1;//标记上能够到达终点。
                }
                if(fabs(output-map[0][i]-dd)<eps)//这里不要忘记。
                {
                    jumpp=min(jumpp,jump[0][i]+1);
                }
            }
        }
4、注意一个点,如果d>42.5的话,就不需要这么些操作了,直接输出42.5 1即可。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
#define eps 1e-5
double map[105][105];
int jump[105][105];
int x[105];
int y[105];
double dis(int x1,int y1,int x2,int y2)
{
    double x=x1-x2;
    double y=y1-y2;
    return sqrt(x*x+y*y);
}
int main()
{
    int n;double d;
    while(~scanf("%d%lf",&n,&d))
    {
        x[0]=0;
        y[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                map[i][j]=0x3f3f3f3f;
                jump[i][j]=1;
                jump[i][i]=0;
            }
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                if(i==j)
                {
                    map[i][j]=0;continue;
                }
                double dd=dis(x[i],y[i],x[j],y[j]);
                if(i==0)dd-=7.50;
                if(dd>d)continue;
                if(dd<0)continue;
                map[i][j]=min(dd,map[i][j]);
            }
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                for(int k=0;k<=n;k++)
                {
                    if(map[j][k]>map[j][i]+map[i][k])
                    {
                        map[j][k]=map[j][i]+map[i][k];
                        jump[j][k]=jump[j][i]+jump[i][k];
                    }
                    if(fabs(map[j][k]-map[j][i]+map[i][k])<eps)
                    {
                        jump[j][k]=min(jump[j][k],jump[j][i]+jump[i][k]);
                    }
                }
            }
        }
        int ok=0;
        int jumpp=0;
        double output=0x3f3f3f3f;
        if(d>=42.50)
        {
            printf("42.50 1\n");
            continue;
        }
        for(int i=0;i<=n;i++)
        {
            double dd=min(min(50-x[i],x[i]+50),min(50-y[i],y[i]+50));
            if(dd>d)continue;
            else
            {
                if(output>map[0][i]+dd)
                {
                    output=map[0][i]+dd;
                    jumpp=jump[0][i]+1;
                    ok=1;
                }
                if(fabs(output-map[0][i]-dd)<eps)
                {
                    jumpp=min(jumpp,jump[0][i]+1);
                }
            }
        }
        if(ok==1)
        {
            printf("%.2lf %d\n",output,jumpp);
        }
        else printf("can't be saved\n");
    }
    return 0;
}
/*
12 5
8 0
9 0
10 0
13 0
18 0
23 0
28 0
33 0
38 0
39 0
43 0
48 0
16 5
8 0
9 0
12 0
17 0
22 0
27 0
32 0
31 0
3 0
45 0
13 0
19 0
25 0
37 0
42 0
47 0
*/




版权声明:0.0小白有写的不好不对的地方请指出~

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

Saving James Bond Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • u010372095
  • u010372095
  • 2015年03月29日 15:04
  • 715

图的搜索和最短路径-Saving James Bond

图的搜索-深度优先搜索DFS 题目 06-图2 Saving James Bond - Easy Version 分析 James从中心岛屿开始跳,这一跳需要进行特殊处理,然后在鳄鱼头上跳的时候,...
  • bobo1356
  • bobo1356
  • 2017年05月24日 10:40
  • 148

hdu 1245 Saving James Bond 策画几何+最短路 最短路求步数最少的路径

#include #include #include #define inf 0x3fffffff #define N 200 #define eps  1e-10 #include u...
  • u011483306
  • u011483306
  • 2014年07月04日 16:05
  • 450

Saving HDU(贪心)

Saving HDUTime Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...
  • oenheng
  • oenheng
  • 2016年07月28日 21:30
  • 416

Saving HDU(贪心)

Saving HDU Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u SubmitStat...
  • qq_32680617
  • qq_32680617
  • 2016年07月10日 17:11
  • 349

HDU-动能定理-人的降落

问题及代码: /* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:E.cpp *作 者:单昕昕 *完成日...
  • MIKASA3
  • MIKASA3
  • 2015年02月03日 14:27
  • 643

HDU:2112 HDU Today(floyd求最短路径+变形题)

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot...
  • zugofn
  • zugofn
  • 2016年08月04日 21:50
  • 500

HDOJ,数论简单入门题目,杭电1262,寻找素数对

这个查结果的思想比较精妙吧。。。。 /***** HDOJ 1262 寻找素数对 ********/ /******** written by C_Shit_Hu **********...
  • hu1020935219
  • hu1020935219
  • 2013年09月15日 01:40
  • 1214

HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,floyd水

题目: 1874 http://acm.hdu.edu.cn/showproblem.php?pid=1874 2444 http://acm.hdu.edu.cn/showproblem.php...
  • hcbbt
  • hcbbt
  • 2013年06月16日 20:25
  • 2282

hdu1317 负权最短路(Floyd+bellman)

首先要读懂题,做这个题时读题看样例琢磨了半天才明白= =; 题目的意思是,一个人从一号房间走到最后一个房间,每个房间有相应的能量值(可正可负),当走到这个房间的时候可以获取这个房间的能量值,如果走到...
  • zs120197
  • zs120197
  • 2016年07月24日 12:45
  • 258
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 1245 Saving James Bond【floyd】
举报原因:
原因补充:

(最多只允许输入30个字)