标签:style blog http color os java io for ar
2 2 10 10 20 20 3 1 1 2 2 1000 1000
1414.2 oh!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define pdi pair<double,int> 16 #define INF 999999 17 using namespace std; 18 const int maxn = 110; 19 int x[maxn],y[maxn],n; 20 double d[maxn],mp[maxn][maxn]; 21 bool done[maxn]; 22 priority_queue< pdi,vector< pdi >, greater< pdi > >q; 23 double calc(int i,int j){ 24 double tmp = (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]); 25 return sqrt(tmp); 26 } 27 void prim(){ 28 while(!q.empty()) q.pop(); 29 for(int i = 1; i <= n; i++){ 30 d[i] = mp[1][i]; 31 done[i] = false; 32 q.push(make_pair(d[i],i)); 33 } 34 done[1] = true;//注意此处 35 double sum = 0; 36 bool flag = true; 37 while(!q.empty()){ 38 int u = q.top().second; 39 double w = q.top().first; 40 q.pop(); 41 if(done[u]) continue; 42 done[u] = true; 43 if(w >= INF){flag = false;break;} 44 sum += w; 45 for(int i = 1; i <= n; i++){ 46 if(!done[i] && d[i] > mp[u][i]){ 47 d[i] = mp[u][i]; 48 q.push(make_pair(d[i],i)); 49 } 50 } 51 } 52 if(flag) printf("%.1f\n",sum*100); 53 else puts("oh!"); 54 } 55 int main() { 56 int t,i,j; 57 double tmp; 58 scanf("%d",&t); 59 while(t--){ 60 scanf("%d",&n); 61 for(i = 1; i <= n; i++) 62 scanf("%d %d",x+i,y+i); 63 for(i = 1; i <= n; i++){ 64 for(j = 1; j <= n; j++){ 65 tmp = calc(i,j); 66 if(tmp < 10 || tmp > 1000) mp[i][j] = mp[j][i] = INF; 67 else mp[i][j] = mp[j][i] = tmp; 68 } 69 } 70 prim(); 71 } 72 return 0; 73 }
标签:style blog http color os java io for ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3940479.html