HDU Saving James Bond (几何+最短路)

思路:

先建双向边:鳄鱼与鳄鱼之间(若距离<=d),圆心与鳄鱼之间(距离-7.5<=d),汇点(n+1)与鳄鱼之间建边(从鳄鱼的坐标能到边界)

跑0~n+1的最短路,因为本题卡精度!!! 所以abs(a-b)<=eps;判相等

#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<algorithm>
using namespace std;
const int N = 2e2 + 5;
const double Inf = 1e9;
const double eps = 1e-4;
typedef pair<double, int> P;
vector<P> v[N];
int n;
double x[N], y[N], jump, di;
inline void add(int x, int y, double c) {
    v[x].push_back(P(c, y));
}
inline double pow(double x) {
    return x * x;
}
inline double getDistance(double x0, double y0, double x1, double y1) {
    return sqrt(pow(x0 - x1) + pow(y0 - y1));
}
void Dijstra(int from, int n) {
    int vis[N], path[N];
    double dis[N];
    for(int i = 1; i <= n + 1; i += 1) {
        dis[i] = Inf;
        vis[i] = false;
        path[i] = (int)Inf;
    }
    priority_queue<P, vector<P>, greater<P> >que;
    dis[from] = 0;
    path[from] = vis[from] = 0;
    que.push(P(0, from));
    while(!que.empty()) {
        int x = que.top().second;
        que.pop();
        if(vis[x])
            continue;
        vis[x] = 1;
        for(auto e : v[x]) {
            int to = e.second;
            double c = e.first;
            if(abs(dis[to] - dis[x] - c) <= eps) {
                if(path[to] > path[x] + 1)
                    path[to] = path[x] + 1;
            } else if(dis[to] >= dis[x] + c) {
                dis[to] = dis[x] + c;
                path[to] = path[x] + 1;
                que.push(P(dis[to], to));
            }
        }
    }
    if(abs(dis[n + 1] - Inf) <= eps)
        printf("can't be saved\n");
    else
        printf("%.2f %d\n", dis[n + 1], path[n + 1]);
}
void BuildEdges() {
    v[0].clear();
    for(int i = 1; i <= n; i += 1) {
        di = sqrt(pow(x[i]) + pow(y[i]));
        if(di - 7.50 <= jump)
            add(0, i, di - 7.50);
        di = min(min(50.0 - x[i], 50.0 - y[i]), min(50.0 + x[i], 50.0 + y[i]));
        if(di <= jump)
            add(i, n + 1, di);
        for(int j = i + 1; j <= n; j += 1) {
            di = getDistance(x[i], y[i], x[j], y[j]);
            if(di <= jump) {
                add(i, j, di);
                add(j, i, di);
            }
        }
    }
}
int main() {
    x[0] = y[0] = 0;
    while(~scanf("%d%lf", &n, &jump)) {
        for(int i = 1; i <= n; i += 1) {
            scanf("%lf%lf", &x[i], &y[i]);
            v[i].clear();
        }
        if(jump >= 42.50) {
            printf("42.50 1\n");
            continue;
        }
        BuildEdges();
        Dijstra(0, n);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值