• Saving James Bond(dijk)


    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1245

    Saving James Bond

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


    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
     
    题意: 湖是以(0,0)为中心的边长为100的正方形,开始小人在湖中心直径为15的岛上,中间的湖有很多的鳄鱼,求小人可以通过鳄鱼跳到岸上吗,小人每次跳跃距离不超过d
    题解: 这个题建图是关键,建好图,用dijk或者是spfa求最短路都可以,将中心岛看成第一个点,将岸边看成是第n+2个点,然后枚举所有的两点之间,如果之间距离小于d就建一条两点之间的边,
    再枚举每个点和中心岛屿和岸边的距离和岛屿和岸边建边,然后求最短路即可
    复习一下dijk
    代码:
      1 #include<algorithm>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<cmath>
      5 using namespace std;
      6 #define INF 0x1fffffff
      7 #define N 110
      8 struct Edge{
      9     int to;
     10     int next;
     11     double v;
     12 }edge[N*N];
     13 
     14 struct point{
     15     double x;
     16     double y;
     17 }p[N];
     18 
     19 int head[N];
     20 int m;
     21 double fd(double x1, double y1, double x2 , double y2)
     22 {
     23     double tm = (x2-x1)*(x2-x1) +(y2-y1)*(y2-y1);
     24     return sqrt(tm);
     25 }
     26 double dis[N];
     27 int cnt[N];
     28 int Enct;
     29 bool vis[N];
     30 void init()
     31 {
     32     Enct = 0;
     33     memset(head,-1,sizeof(head));
     34     memset(vis, 0 , sizeof(vis));
     35     for(int i = 0 ;i < N ; i++){
     36         dis[i] = INF;
     37         cnt[i] = INF;
     38     }
     39     cnt[0] = 1;
     40     dis[0] = 0;
     41 }
     42 void add(int from , int to , double v)
     43 {
     44     if(v>m) return ;
     45     edge[Enct].to = to;
     46     edge[Enct].v = v;
     47     edge[Enct].next = head[from];
     48     head[from] = Enct++;
     49     edge[Enct].to = from;
     50     edge[Enct].v = v;
     51     edge[Enct].next = head[to];
     52     head[to] = Enct++;
     53 }
     54 int n;
     55 
     56 void dijk()
     57 {
     58     for(int i =0 ;i < n ;i++)
     59     {
     60     //for(int j = 0; j < n; j++) printf("%.2lf ", dis[j]); puts("");
     61         int Min = INF ;
     62         int Minc = INF ;
     63         int k = -1;
     64         for(int j = 0 ; j < n ; j++)
     65         {
     66             if(!vis[j]&&dis[j]<=Min)
     67             {
     68                 if(dis[j]<Min) 
     69                 {
     70                     Min = dis[j];
     71                     k = j;
     72                 }
     73                 else if(dis[j]==Min&&cnt[j]<Minc)
     74                 {
     75                     Minc = cnt[j];
     76                     k = j;
     77                 }
     78             }
     79         }
     80         //printf("%d 
    ",k);
     81         if(Min == INF) return ;
     82         vis[k] = 1;
     83         for( int j = head[k] ; j != -1 ; j = edge[j].next)
     84         {
     85             Edge e = edge[j];
     86             if(!vis[e.to]&&(dis[k]+e.v)==dis[e.to]&&cnt[k]+1<cnt[e.to])
     87             {
     88                 cnt[e.to] = cnt[k]+1;
     89             }
     90             if(!vis[e.to]&&dis[k]+e.v<dis[e.to]) 
     91             {
     92                 cnt[e.to] = cnt[k]+1;
     93                 dis[e.to] = dis[k]+e.v;
     94             }
     95         }
     96     }
     97 }
     98 
     99 int main()
    100 {
    101     while(~scanf("%d%d",&n,&m))
    102     {
    103         init();
    104         for(int i = 1 ; i <= n ;i++)
    105         {
    106             double x, y;
    107             scanf("%lf%lf",&x,&y);
    108             p[i].x = x;
    109             p[i].y = y;
    110             double dd = 50-max(fabs(x), fabs(y));
    111             if(dd <= m) add(i, n+1, dd); 
    112             //if((x>=50-m&&x>y)||(x<=-(50-m)&&x<y)) add(i,n+1,(50-abs(x)));
    113             //if((y>=50-m&&x<y)||(y<=-(50-m)&&x>y)) add(i,n+1,(50-abs(y)));
    114             for(int j = 0 ; j < i ; j++)
    115             {
    116                 double flag = fd(p[i].x,p[i].y,p[j].x,p[j].y);
    117                 if(flag <= m) add(i,j,flag);
    118             }
    119         }
    120         for(int i = 1; i <= n; i++)
    121         {
    122             double tm = fd(p[i].x, p[i].y, 0, 0);
    123             if(tm - 7.5 <= m) add(0, i, tm - 7.5);
    124         }
    125         //        for(int i = 0; i < n+2; i++)
    126         //      {
    127         //    printf("%d:", i); 
    128         //for(int j = head[i]; j != -1; j = edge[j].next) printf("(%d %.2lf) ", edge[j].to, edge[j].v);
    129         //        puts("");
    130         //    }
    131 
    132         n += 2;
    133         dijk();
    134         if(dis[n-1]==INF) puts("can't be saved");
    135         else 
    136         printf("%.2f %d
    ",dis[n-1],cnt[n-1]-1);
    137     }
    138     return 0;
    139 }
  • 相关阅读:
    winfrom 获取当前系统时间
    netcore3.1API+efcore快速搭建
    php
    php
    php
    php-array的相关函数使用
    php-正则表达式
    vim的复制与粘贴
    vim的多窗口和文件切换操作
    laravel教程中出现500问题
  • 原文地址:https://www.cnblogs.com/shanyr/p/4756515.html
Copyright © 2020-2023  润新知