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