zoukankan      html  css  js  c++  java
  • New Game! (最短路+建图)

    New Game!

    https://www.nowcoder.com/acm/contest/201/L

    题目描述

    Eagle Jump公司正在开发一款新的游戏。Hifumi Takimoto作为其中的员工,获得了提前试玩的机会。现在她正在试图通过一个迷宫。
    这个迷宫有一些特点。为了方便描述,我们对这个迷宫建立平面直角坐标系。迷宫中有两条平行直线 L1:Ax+By+C1=0, L2:Ax+By+C2=0,还有 n 个圆 。角色在直线上、圆上、园内行走不消耗体力。在其他位置上由S点走到T点消耗的体力为S和T的欧几里得距离。
    Hifumi Takimoto想从 L1 出发,走到 L2 。请计算最少需要多少体力。

    输入描述:

    第一行五个正整数 n,A,B,C
    1
    ,C
    2
     (1≤ n ≤ 1000, -10000 ≤ A,B,C
    1
    ,C
    2
     ≤ 10000),其中 A,B 不同时为 0。
    接下来 n 行每行三个整数 x,y,r(-10000 ≤ x,y ≤ 10000, 1≤ r ≤ 10000) 表示一个圆心为 (x,y),半径为 r 的圆。

    输出描述:

    仅一行一个实数表示答案。与正确结果的绝对误差或者相对误差不超过 10
    -4
     即算正确。

    输入

    2 0 1 0 -4
    0 1 1
    1 3 1

    输出

    0.236068


    可以把圆看成一个点,利用点到直线的距离公式和点到点的距离公式,求出各个点之间的距离,跑一遍最短路即可
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<string>
     5 #include<stack>
     6 #include<algorithm>
     7 #include<cmath>
     8 #define PI acos(-1.0)
     9 #define INF 0x3f3f3f3f
    10 using namespace std;
    11 
    12 int n;
    13 double A,B,C1,C2;
    14 struct Circle{
    15     double x,y,r;
    16 }p[1005];
    17 
    18 double map[1005][1005];
    19 double vis[1005];
    20 double dis[1005];
    21 
    22 double dist1(Circle a,Circle b){
    23     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))-a.r-b.r;
    24 }
    25 
    26 double dist2(double a,double b,double c,Circle d){
    27     return (fabs(a*d.x+b*d.y+c)/sqrt(a*a+b*b)-d.r);
    28 }
    29 
    30 void Dijstra(){
    31     for(int i=1;i<=n+2;i++){
    32         dis[i]=map[1][i];
    33         vis[i]=0;
    34     }
    35     vis[1]=1;
    36     dis[1]=0;
    37     for(int i=1;i<n+2;i++){
    38         double minn=INF;
    39         int pos=0;
    40         for(int j=1;j<=n+2;j++){
    41             if(!vis[j]&&dis[j]<minn){
    42                 minn=dis[j];
    43                 pos=j;
    44             }
    45         }
    46         vis[pos]=1;
    47         for(int j=1;j<=n+2;j++){
    48             if(!vis[j]&&dis[j]>minn+map[pos][j]){
    49                 dis[j]=minn+map[pos][j];
    50             }
    51         }
    52     }
    53     cout<<dis[n+2]<<endl;
    54 }
    55 
    56 int main(){
    57     std::ios::sync_with_stdio(false);
    58     cin>>n>>A>>B>>C1>>C2;
    59     for(int i=0;i<1005;i++){
    60         for(int j=0;j<1005;j++){
    61             map[i][j]=INF;
    62         }
    63     }
    64     for(int i=2;i<=n+1;i++){
    65         cin>>p[i].x>>p[i].y>>p[i].r;
    66     }
    67     double tmp;
    68     for(int i=2;i<=n+1;i++){
    69         for(int j=i+1;j<=n+1;j++){
    70             tmp=dist1(p[i],p[j]);
    71             if(tmp>0)
    72                 map[i][j]=map[j][i]=tmp;
    73             else
    74                 map[i][j]=map[j][i]=0;
    75         }
    76     }
    77     for(int i=2;i<=n+1;i++){
    78         tmp=dist2(A,B,C1,p[i]);
    79         if(tmp>0)
    80             map[1][i]=map[i][1]=tmp;
    81         else
    82             map[1][i]=map[i][1]=0;
    83         tmp=dist2(A,B,C2,p[i]);
    84         if(tmp>0)
    85             map[n+2][i]=map[i][n+2]=tmp;
    86         else
    87             map[n+2][i]=map[i][n+2]=0;
    88     }
    89     map[1][n+2]=map[n+2][1]=fabs(C1-C2)/sqrt(A*A+B*B);
    90     Dijstra();
    91 
    92 }
    View Code
  • 相关阅读:
    R vs Python:构建data.frame、读取csv与统计描述
    R语言学习笔记:使用reshape2包实现整合与重构
    Python学习笔记:lambda表达式
    Python学习笔记:startswith & endswith 判断开头结尾是否为指定字符串
    Python学习笔记:一手漂亮的Python函数
    电信行业数据挖掘分析
    Oracle学习笔记:实现select top N的方法
    Oracle学习笔记:ORA-22992 cannot use LOB locators selected from remote tables
    Linux学习笔记:ls和ll命令
    vb常用命名空间
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9735995.html
Copyright © 2011-2022 走看看