牛客网补题 New Game!(原Wannafly summer camp day2原题)

思路:这个题在秦皇岛的时候好像没有写出来,反正我是没有写出来,题解是听懂了;把直线和圆都看做一个结点,圆和直线用点到直线的距离与半径差求出来,圆和圆之间用点和点之间的距离和半径差表示,最后最短路跑一遍就行了。

#include<cstdio>
#include<vector>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int vis[1005];
double mlen[1005];
const double maxn = 1e9 + 10;
double map[1005][1005];
int n, a, b, c1, c2;
struct Node{
    int x, y, r;
    Node(){}
    Node(int _x, int _y, int _r) :x(_x), y(_y), r(_r){}
}ya[1005];

void dijkstra(){
    memset(vis, 0, sizeof vis);
    
    for (int i = 0; i <= n + 1; i++)
        mlen[i] = map[0][i];
    mlen[0] = 0;
    /*for (int i = 0; i <= n + 1; i++)
        cout << mlen[i] << " * ";
        */
    for (int t = 0; t <= n + 1; t++){
        double ma = maxn; int p;
        for (int i = 0; i <= n + 1; i++){
            if (mlen[i] <= ma&&!vis[i]){ ma = mlen[i]; p = i; }
        }
        vis[p] = 1;
        //cout << mlen[p] << " " << p << endl;
        for (int i = 0; i <= n + 1; i++){
            if (p != i&&!vis[i] && mlen[p] + map[p][i] < mlen[i]){
                mlen[i] = mlen[p] + map[p][i];
                //cout << p << " " << i << " " << mlen[i] << endl;
            }
        }
    }
}
int main(){
    
    //memset(map, maxn, sizeof map);
    
    vector<Node>vec;

    scanf("%d%d%d%d%d", &n, &a, &b, &c1, &c2);
    
    map[0][n + 1] = map[n + 1][0] = abs(c1 - c2)*1.0 / sqrt(a*a + b*b);
    for (int i = 0; i <= n + 1; i++)
    for (int j = 0; j <= n + 1; j++)
        map[i][j] = maxn;

    int x, y, r;
    for (int i = 1; i <= n; i++){
        scanf("%d%d%d", &x, &y, &r);
        vec.push_back(Node(x, y, r));
    }
    
    for (int i = 0; i < vec.size(); i++){
        double len = abs(a*vec[i].x + b*vec[i].y + c1)*1.0 / sqrt(a*a + b*b) - vec[i].r*1.0;
        if (len < 0)len = 0;
        map[0][i + 1] = len;
        map[i + 1][0] = len;
    }
    for (int i = 0; i < vec.size(); i++){
        double len = abs(a*vec[i].x + b*vec[i].y + c2)*1.0 / sqrt(a*a + b*b) - vec[i].r*1.0;
        if (len < 0)len = 0;
        map[n+1][i + 1] = len;
        map[i + 1][n+1] = len;
    }

    for (int i = 0; i < n; i++){
        for (int j = i + 1; j < n; j++){
            double len = sqrt(pow(vec[i].x - vec[j].x, 2) + pow(vec[i].y - vec[j].y, 2));
            len = len - vec[i].r - vec[j].r;
            if (len < 0)len = 0;
            map[i + 1][j + 1] = len;
            map[j + 1][i + 1] = len;
        }
    }
    /*for (int i = 0; i <= n + 1; i++){
        for (int j = 0; j <= n + 1; j++)
            cout << map[i][j] << " ";
        cout << endl;
    }*/
    dijkstra();
    printf("%.6lf\n", mlen[n + 1]);

    return 0;
}

 

转载于:https://www.cnblogs.com/zengguoqiang/p/9736131.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值