耐克店 和 苹果店必须相连
Sample Input
4
2 3
0 0
1 0
0 -1
1 -1
0
Sample Output
3.41
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using namespace std ; 8 9 const int INF=0x3f3f3f3f; 10 const int MAXN=110; 11 bool vis[MAXN]; 12 double lowc[MAXN]; 13 int n ; 14 double cost[MAXN][MAXN] ; 15 16 struct poin 17 { 18 int x ; 19 int y ; 20 }p[MAXN]; 21 22 double Prim()//点是0~n-1 23 { 24 double ans=0; 25 memset(vis,false,sizeof(vis)); 26 vis[0]=true; 27 for(int i=1;i<n;i++)lowc[i]=cost[0][i]; 28 for(int i=1;i<n;i++) 29 { 30 double minc=INF; 31 int p=-1; 32 for(int j=0;j<n;j++) 33 if(!vis[j]&&minc>lowc[j]) 34 { 35 minc=lowc[j]; 36 p=j; 37 } 38 if(minc==INF)return -1;//原图不连通 39 ans+=minc; 40 vis[p]=true; 41 for(int j=0;j<n;j++) 42 if(!vis[j]&&lowc[j]>cost[p][j]) 43 lowc[j]=cost[p][j]; 44 } 45 return ans; 46 } 47 48 int main() 49 { 50 51 // freopen("in.txt","r",stdin) ; 52 while(scanf("%d" , &n) != EOF) 53 { 54 if (n == 0) 55 break ; 56 int i , j ; 57 int pp , qq ; 58 scanf("%d %d" , &pp , &qq) ; 59 for (i = 0 ; i < n ; i++) 60 scanf("%d %d" , &p[i].x , &p[i].y) ; 61 for (i = 0 ; i < n ; i++) 62 for (j = i+1 ; j < n ; j++) 63 { 64 double t = sqrt((double)(p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y)) ; 65 cost[i][j] = t ; 66 cost[j][i] = t ; 67 } 68 double k = cost[pp-1][qq-1] ; 69 cost[pp-1][qq-1] = 0 ; 70 cost[qq-1][pp-1] = 0 ; 71 k += Prim() ; 72 printf("%.2lf\n" , k) ; 73 74 75 } 76 return 0 ; 77 }
时间: 2024-10-18 10:39:32