题意:
最小生成树,有的边已经不用考虑:
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 760
double w[N][N],x[N],y[N],key[N];
int pre[N], hash[N], n, m;
inline double getDist(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double prim() {
double u = 0xFFFFFFF;
double sum = 0;
memset(hash, 0, sizeof(hash));
for(int i=1; i<=n; i++) {
key[i] = w[1][i];
}
hash[1] = 1;
for(int i=2; i<=n; i++) {
u = 0xFFFFFFF;
int x;
for(int j=1; j<=n; j++) {
if(!hash[j])
if(key[j]<u) {
u = key[j];
x = j;
}
}
sum += key[x];
hash[x] = 1;
for(int j=1; j<=n; j++) {
if(key[j] > w[x][j]) {
key[j] = w[x][j];
}
}
}
return sum;
}
int main(){
int u, v;
while(~scanf("%d", &n)) {
for(int i=1; i<=n; i++)
scanf("%lf%lf", &x[i], &y[i]);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(i != j) {
w[i][j] = getDist(x[i], y[i], x[j], y[j]);
}
scanf("%d", &m);
for(int i=0; i<m; i++) {
scanf("%d%d", &u, &v);
w[u][v]=w[v][u]=0;
}
printf("%.2f\n", prim());
}
return 0;
}