标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17913 Accepted Submission(s): 5593
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <algorithm> 8 #include <cstring> 9 #include <cctype> 10 #include <cstdlib> 11 #include <cmath> 12 #include <ctime> 13 using namespace std; 14 15 const int SIZE = 105; 16 int FATHER[SIZE],N,M,NUM; 17 int MAP[SIZE][SIZE]; 18 struct Node 19 { 20 int from,to; 21 double cost; 22 }G[SIZE * SIZE]; 23 struct 24 { 25 int x,y; 26 }TEMP[SIZE]; 27 28 void ini(void); 29 int find_father(int); 30 void unite(int,int); 31 bool same(int,int); 32 void kruskal(void); 33 bool comp(const Node &,const Node &); 34 double dis(int,int,int,int); 35 int main(void) 36 { 37 int t; 38 double temp; 39 40 scanf("%d",&t); 41 while(t --) 42 { 43 scanf("%d",&N); 44 ini(); 45 for(int i = 1;i <= N;i ++) 46 scanf("%d%d",&TEMP[i].x,&TEMP[i].y); 47 for(int i = 1;i <= N;i ++) 48 for(int j = i + 1;j <= N;j ++) 49 { 50 temp = sqrt(dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y)); 51 if(temp >= 10 && temp <= 1000) 52 { 53 G[NUM].from = i; 54 G[NUM].to = j; 55 G[NUM].cost = temp * 100; 56 NUM ++; 57 } 58 } 59 sort(G,G + NUM,comp); 60 kruskal(); 61 } 62 63 return 0; 64 } 65 66 void ini(void) 67 { 68 NUM = 0; 69 for(int i = 1;i <= N;i ++) 70 FATHER[i] = i; 71 } 72 73 int find_father(int n) 74 { 75 if(FATHER[n] == n) 76 return n; 77 return FATHER[n] = find_father(FATHER[n]); 78 } 79 80 void unite(int x,int y) 81 { 82 x = find_father(x); 83 y = find_father(y); 84 85 if(x == y) 86 return ; 87 FATHER[x] = y; 88 } 89 90 bool same(int x,int y) 91 { 92 return find_father(x) == find_father(y); 93 } 94 95 bool comp(const Node & a,const Node & b) 96 { 97 return a.cost < b.cost; 98 } 99 100 void kruskal(void) 101 { 102 int count = 0; 103 double ans = 0; 104 105 for(int i = 0;i < NUM;i ++) 106 if(!same(G[i].from,G[i].to)) 107 { 108 unite(G[i].from,G[i].to); 109 count ++; 110 ans += G[i].cost; 111 if(count == N - 1) 112 break; 113 } 114 if(count == N - 1) 115 printf("%.1f\n",ans); 116 else 117 puts("oh!"); 118 } 119 120 double dis(int x_1,int y_1,int x_2,int y_2) 121 { 122 return pow(x_1 - x_2,2) + pow(y_1 - y_2,2); 123 }
标签:
原文地址:http://www.cnblogs.com/xz816111/p/4549533.html