Comet OJ 熱身賽(E題)(處理+最短路算法)


dijkstra

 

42.86%

Total Submission:189

Total Accepted:81

題目描述

 

Eagle Jump公司正在開發一款新的游戲。瀧本一二三作為其員工,獲得了提前試玩的機會。現在她正在試圖通過一個迷宮。

這個迷宮有一些特點。為了方便描述,我們對這個迷宮建立平面直角坐標系。迷宮中有兩條平行直線 L_1:Ax+By+C_1=0L1:Ax+By+C1=0, L_2:Ax+By+C_2=0L2:Ax+By+C2=0,還有 nn 個圓 C_i:(x-x_i)^2+(y-y_i)^2={r_i}^2Ci:(xxi)2+(yyi)2=ri2。角色在直線上、圓上、圓內行走不消耗體力。在其他位置上由SS點走到TT點消耗的體力為SS和TT的歐幾里得距離。

瀧本一二三想從 L_1L1 出發,走到 L_2L2 。請計算最少需要多少體力。

 

 
 

輸入描述

 

第一行五個正整數 n,A,B,C_1,C_2n,A,B,C1,C2 (1\le n \le 1000, -10000 \le A,B,C_1,C_2 \le 10000)(1n1000,10000A,B,C1,C210000),其中 A,BA,B 不同時為 0。

接下來 nn 行每行三個整數 x,y,r(-10000 \le x,y \le 10000, 1\le r \le 10000)x,y,r(10000x,y10000,1r10000) 表示一個圓心為 (x,y)(x,y),半徑為 rr 的圓。

 

輸出描述

 

僅一行一個實數表示答案。與標准答案的絕對誤差或者相對誤差不超過 10^{-4}104 即算正確。

 

樣例輸入 1 

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

樣例輸出 1

0.236068

題意:給定兩個平行的直線,直線中間有若干個圓,點在直線和圓上行走不消耗能力。問從第一條直線走到第二條直線最小需要消耗多少能量。
距離即代表消耗的能力。
思路:
1、首先處理每一個圓心到其他圓心和直線的距離,以各個圓心點和直線縮成點構成一個圖,然后根據距離跑最短路算法即可。
2、注意距離減去半徑的時候如果是負數要賦值為0。
細節見我的代碼;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
/*** TEMPLATE CODE STARTS HERE ***/
const int maxn=10008;
const int INF= 0x3f3f3f3f;
struct Node
{
    int to;
    double dist;
    Node(){}
    Node(int _n,double _d)
    {
        to=_n;
        dist=_d;
    }
    bool operator < (const Node x ) const
    {
        return dist > x.dist;
    }
};
priority_queue<Node> heap;
vector <Node> Map[maxn];
double dis[maxn];
int t,n,star;
void dijkstra (int strat)
{
//    memset(dis,INF,sizeof(dis));
    repd(i,1,maxn-1)
    {
        dis[i]=9999999999.0;
    }
    dis[strat]=0;
    heap.push(Node(strat,dis[strat]));
    while(!heap.empty())
    {
        Node x= heap.top();
        heap.pop();
        int LEN=Map[x.to].size();
        rep(i,0,LEN)
        {
            Node now =Map[x.to][i];
            if(dis[now.to]>x.dist+now.dist)
            {
                dis[now.to]=x.dist+now.dist;
                heap.push(Node(now.to,dis[now.to]));
            }
        }
    }

}
int a,b,c1,c2;
struct yuan
{
    int x,r,y;
}y[maxn];
double getdis(int id)
{
    double res=0.00000;
    res=max(0.000,fabs(a*y[id].x+b*y[id].y+c1)*1.000/sqrt(a*a+b*b));
    return res;

}
double getdis2(int id)
{
    double res=0.00000;
    res=max(0.00,fabs(a*y[id].x+b*y[id].y+c2)*1.000/sqrt(a*a+b*b));
    return res;

}
int main()
{
//         scanf("%d %d %d",&n,&t,&star);
//         int a,b,d;
//         repd(i,1,t)
//         {
//             scanf("%d %d %d",&a,&b,&d);
//             Map[a].pb(Node(b,d));
// //            Map[b].pb(Node(a,d));
//         }
//         dijkstra(star);
//         printf("%d\n",ans);
    gg(n);
    gg(a);gg(b);gg(c1);gg(c2);
    repd(i,3,n+2)
    {
        gg(y[i].x);
        gg(y[i].y);
        gg(y[i].r);
    }
    repd(i,3,n+2)
    {
        double dt=max(getdis(i)-y[i].r,0.00);
        Map[1].push_back(Node(i,dt));
        Map[i].push_back(Node(1,dt));
    }
    repd(i,3,n+2)
    {
        double dt=max(getdis2(i)-y[i].r,0.00);
        Map[2].push_back(Node(i,dt));
        Map[i].push_back(Node(2,dt));
    }
    repd(i,3,n+2)
    {
        repd(j,3,n+2)
        {
            if(i==j)
            {
                continue;
            }else
            {
                double dt=max(0.000,-y[i].r-y[j].r+sqrt((y[i].x-y[j].x)*(y[i].x-y[j].x)+(y[i].y-y[j].y)*(y[i].y-y[j].y)));
                Map[i].push_back(Node(j,dt));
                Map[j].push_back(Node(i,dt));
            }
        }
    }
    dijkstra(1);
    printf("%.6lf\n",dis[2] );
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

 

 
              


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM