Saving James Bond HDU - 1245 (最短路)

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

这个题就是建图以后最短路,直接写了,但是发现一直T,后来我回过头来看,我似乎一直没有考虑一个问题,我建的这个图应该是无向的还是有向的,后来仔细想想在起点和终点有关的连边一定得是有向的,应该这些点绝对不会重复走的,后来我把这个地方修改了一下时限3秒竟然92ms就过了…

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#define INF 0x7fffffff
#define N 105
using namespace std;
const double eps=1e-4;
struct point
{
    int x,y;
}p[N];
int n;
double d;
double getDis(int num1,int num2)
{
    int xx=p[num1].x-p[num2].x;
    int yy=p[num1].y-p[num2].y;
    return sqrt(xx*xx+yy*yy);
}
vector<int>graph[N];
struct node
{
    int to;
    double dis;
}edge[20005];
int k;
void addEdge(int x,int y,double w)
{
    edge[k].to=y;
    edge[k].dis=w;
    graph[x].push_back(k);
    k++;
}
double dis[N];
int step[N];
queue<int>que;
bool inQ[N];
void spfa()
{
    for(int i=0;i<=n+1;i++)
        dis[i]=(double)INF,step[i]=INF;
    dis[0]=0;
    step[0]=0;
    que.push(0);
    inQ[0]=true;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        inQ[u]=false;
        for(int i=0;i<graph[u].size();i++)
        {
            int v=edge[graph[u][i]].to;
            double w=edge[graph[u][i]].dis;
            if(abs(dis[v]-dis[u]-w)<=eps&&step[v]>step[u]+1)
            {
                step[v]=step[u]+1;
                if(!inQ[v])
                {
                    que.push(v);
                    inQ[v]=true;
                }
            }
            else
                if(dis[v]>dis[u]+w)
                {
                    dis[v]=dis[u]+w;
                    step[v]=step[u]+1;
                    if(!inQ[v])
                    {
                        que.push(v);
                        inQ[v]=true;
                    }
                }
        }
    }
}
void init(int x)
{
    for(int i=0;i<=x;i++)
        graph[i].clear();
    k=0;
}
int main()
{
    int x,y;
    p[0].x=0;
    p[0].y=0;
    while(scanf("%d%lf",&n,&d)==2)
    {
        init(n+1);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            p[i].x=x;
            p[i].y=y;
        }
        for(int i=1;i<=n;i++)
        {
            double dis1=getDis(i,0)-7.5;
            if(dis1<=d)//能否建边就看邦德能否跳过去
                addEdge(0,i,dis1);
            int xx=min(p[i].x+50,50-p[i].x);
            int yy=min(p[i].y+50,50-p[i].y);
            int minn=min(xx,yy);
            //cout<<minn<<endl;
            if(minn<=d)
                addEdge(i,n+1,(double)minn);
            for(int j=1;j<i;j++)
            {
                dis1=getDis(i,j);
                if(abs(dis1-d)<=eps||dis1<d)
                {
                    addEdge(j,i,dis1);                  
                    addEdge(i,j,dis1);
                }
            }
        }
        /*for(int i=0;i<=n+1;i++)
        {
            for(int j=0;j<graph[i].size();j++)
            {
                cout<<edge[graph[i][j]].to<<"/"<<edge[graph[i][j]].dis<<" ";
            }
            cout<<endl;
        }*/
        //cout<<"haha"<<endl;
        spfa();
        if(dis[n+1]==INF)
            printf("can't be saved\n");
        else
            printf("%0.2lf %d\n",dis[n+1],step[n+1]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值