暢通工程再續
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 32093 Accepted Submission(s): 10532
每組數據首先是一個整數C(C <= 100),代表小島的個數,接下來是C組座標,代表每個小島的座標,這些座標都是 0 <= x, y <= 1000的整數。
#include <bits/stdc++.h>
using namespace std;
struct edge{
int from, to, val;
bool operator < (const edge& x) const{
return val < x.val;
}
}a[11111];
int p[11111], x[111], y[111];
int findset(int x){
return p[x] == x ? x : p[x] = findset(p[x]);
}
int main(){
int T;
scanf("%d", &T);
while(T--){
int n, u, v, num;
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d %d", &x[i], &y[i]);
}
num = 0;
for(int i = 1; i <= n; ++i){
for(int j = i + 1; j <= n; ++j){
a[++num].from = i;
a[num].to = j;
a[num].val = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
}
}
for(int i = 1; i <= n; ++i){
p[i] = i;
}
sort(a + 1, a + 1 + num);
int tot = 1, pos = 1;
double ans = 0;
while(tot < n && pos <= num){
u = findset(a[pos].from);
v = findset(a[pos].to);
if(u != v && a[pos].val <= 1000000 && a[pos].val >= 100){
p[u] = v;
tot++;
ans += sqrt(a[pos].val);
}
pos++;
}
ans *= 100;
if(tot < n){
printf("oh!\n");
}
else{
printf("%.1f\n", ans);
}
}
}
/*
題意:
100個城市,給出它們的座標,求最小生成樹。
思路:
最小生成樹裸題,並查集維護一下兩點的連通性。
*/