思路:
我们发现只要求出每个星域之间的距离就行了,然后跑最短路
c o d e code code
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int t, n, tot, head[110];
double xa, ya, za, x0, yy, z0, dis[110];
bool v[110];
struct node {
double x, y, z, d;
}a[110];
struct abc {
int to, next;
double w;
}b[100010];
priority_queue<pair<double, int> > q;
double jis(double x, double y, double z, double x1, double y1, double z1) {
return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1) + (z - z1) * (z - z1));
}
void add(int x, int y, double w) {
b[++ tot] = (abc) { y, head[x], w };
head[x] = tot;
}
void dij(int s) {
memset(v, 0, sizeof(v));
for(int i = 0; i <= n + 1; i ++) dis[i] = 1000000000.0;
dis[s] = 0;
while(!q.empty()) q.pop();
q.push(make_pair(0.0, s));
while(!q.empty()) {
int x = q.top().second;
q.pop();
if(v[x]) continue;
v[x] = 1;
for(int i = head[x]; i; i = b[i].next) {
int y = b[i].to;
if(dis[y] > dis[x] + b[i].w) {
dis[y] = dis[x] + b[i].w;
q.push(make_pair(-dis[y], y));
}
}
}
}
int main() {
freopen("warp.in", "r", stdin);
freopen("warp.out", "w", stdout);
scanf("%d", &n);
while(n != -1) {
memset(head, 0, sizeof(head)), tot = 0;
for(int i = 1; i <= n; i ++) {
scanf("%lf%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z, &a[i].d);
for(int j = 1; j <= i - 1; j ++) {
double di = jis(a[i].x, a[i].y, a[i].z, a[j].x, a[j].y, a[j].z) - a[i].d - a[j].d;
if(di < 0) di = 0;
add(i, j, di), add(j, i, di);
}
}
scanf("%lf%lf%lf", &xa, &ya, &za);
scanf("%lf%lf%lf", &x0, &yy, &z0);
for(int i = 1; i <= n; i ++) {
double di = jis(xa, ya, za, a[i].x, a[i].y, a[i].z) - a[i].d;
if(di < 0) di = 0;
add(0, i, di), add(i, 0, di);
}
for(int i = 1; i <= n; i ++) {
double di = jis(x0, yy, z0, a[i].x, a[i].y, a[i].z) - a[i].d;
if(di < 0) di = 0;
add(n + 1, i, di), add(i, n + 1, di);
}
double di = jis(xa, ya, za, x0, yy, z0);
add(0, n + 1, di), add(n + 1, 0, di);
dij(0);
int k = round(dis[n + 1] * 10);
printf("%d\n", k);
scanf("%d", &n);
}
return 0;
}