Saving James Bond HDU - 1245(多源点 floyd)

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

题意:
你在半径为7.5的圆上。被100*100的正方形包围。你在圆上移动,区域内有鳄鱼,你最多跳h距离,可以踩在鳄鱼上走。问最短距离,最少多少步跳出正方形。
思路:
多源点多汇点。开一个超级源点和超级汇点。算出能互相跳到的鳄鱼之间距离。然后跑最短路算法就好了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int maxn = 105;
const double INF = 1e9;

double dist(double x1,double y1,double x2,double y2)
{
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

double d[maxn][maxn],X[maxn],Y[maxn];
int step[maxn][maxn];

int main()
{
    int n;
    double h;
    while(~scanf("%d%lf",&n,&h))
    {
        int cnt = 1;
        for(int i = 1;i <= n;i++)
        {
            double x,y;scanf("%lf%lf",&x,&y);
            if(fabs(x) <= 7.5 && fabs(y) <= 7.5)
            {
                continue;
            }
            X[cnt] = x, Y[cnt] = y;
            cnt++;
        }
        n = cnt;
        
        if(h >= 42.5)
        {
            printf("42.5 1\n");
            continue;
        }
        
        for(int i = 0;i <= n;i++)
        {
            for(int j = 0;j <= n;j++)
            {
                d[i][j] = i == j ? 0 : INF;
                step[i][j] = 0;
            }
        }
        
        for(int i = 1;i < n;i++)
        {
            for(int j = i + 1;j < n;j++)
            {
                double tmp = dist(X[i],Y[i],X[j],Y[j]);
                if(tmp <= h)
                {
                    step[i][j] = step[j][i] = 1;
                    d[i][j] = d[j][i] = tmp;
                }
                else
                {
                    step[i][j] = step[j][i] = 0;
                    d[i][j] = d[j][i] = INF;
                }
            }
        }
        
        
        for(int i = 1;i < n;i++)
        {
            if(h + 7.5 >= dist(X[i],Y[i],0,0))
            {
                step[0][i] = step[i][0] = 1;
                d[0][i] = d[i][0] = dist(X[i],Y[i],0,0) - 7.5;
            }
            if(50 - X[i] <= h || X[i] + 50 <= h || 50 - Y[i] <= h || 50 + Y[i] <= h)
            {
                step[i][n] = step[n][i] = 1;
                d[i][n] = d[n][i] = min(min(50 - X[i],X[i] + 50),min(50 - Y[i],50 + Y[i]));
            }
        }
        
        for(int k = 0;k <= n;k++)
        {
            for(int i = 0;i <= n;i++)
            {
                for(int j = 0;j <= n;j++)
                {
                    if(d[i][k] < INF && d[k][j] < INF && d[i][j] > d[i][k] + d[k][j])
                    {
                        d[i][j] = d[i][k] + d[k][j];
                        step[i][j] = step[i][k] + step[k][j];
                    }
                }
            }
        }
        
        if(d[0][n] == INF)
        {
            printf("can't be saved\n");
        }
        else
        {
            printf("%.2f %d\n",d[0][n],step[0][n]);
        }
    }
    return 0;
}

发布了594 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/103981692
今日推荐