标签:des style blog http color io os ar java
2 4 2 0 0 1 1 2 0 3 1 4 3 0 0 1 1 1 0 0 1
9.66 9.00
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 INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 double g[maxn][maxn],d[maxn],px[maxn],py[maxn],dp[maxn][maxn]; 19 int p[maxn],n,m; 20 vector<int>e[maxn]; 21 bool used[maxn][maxn]; 22 double getDis(int i,int j) { 23 double tmp = (px[i] - px[j])*(px[i] - px[j]); 24 tmp += (py[i] - py[j])*(py[i] - py[j]); 25 return sqrt(tmp); 26 } 27 double prim() { 28 bool vis[maxn] = {false}; 29 for(int i = 1; i <= n; ++i) { 30 d[i] = INF; 31 p[i] = -1; 32 } 33 d[1] = 0; 34 double MST = 0; 35 while(true) { 36 double minVal = INF; 37 int idx = -1; 38 for(int i = 1; i <= n; ++i) 39 if(!vis[i] && minVal > d[i]) minVal = d[idx = i]; 40 if(idx == -1 || minVal >= INF) break; 41 if(p[idx] > -1) { 42 e[idx].push_back(p[idx]); 43 e[p[idx]].push_back(idx); 44 used[idx][p[idx]] = used[p[idx]][idx] = true; 45 } 46 vis[idx] = true; 47 MST += minVal; 48 for(int i = 1; i <= n; ++i) 49 if(!vis[i] && d[i] > g[idx][i]) { 50 d[i] = g[idx][i]; 51 p[i] = idx; 52 } 53 } 54 return MST; 55 } 56 double dfs(int cur,int u,int fa){ 57 double minV = INF; 58 for(int i = e[u].size()-1; i >= 0; --i){ 59 if(e[u][i] == fa) continue; 60 double tmp = dfs(cur,e[u][i],u); 61 minV = min(tmp,minV); 62 dp[u][e[u][i]] = dp[e[u][i]][u] = min(tmp,dp[u][e[u][i]]); 63 } 64 if(cur != fa) minV = min(minV,g[cur][u]); 65 return minV; 66 } 67 int main() { 68 int cs; 69 scanf("%d",&cs); 70 while(cs--){ 71 scanf("%d %d",&n,&m); 72 for(int i = 0; i <= n; ++i) e[i].clear(); 73 for(int i = 1; i <= n; ++i) 74 scanf("%lf %lf",px+i,py+i); 75 for(int i = 1; i <= n; ++i){ 76 for(int j = 1; j <= n; ++j){ 77 if(i == j) g[i][j] = 0; 78 else g[i][j] = getDis(i,j); 79 dp[i][j] = INF; 80 } 81 } 82 double MST = prim(),ans = MST; 83 for(int i = 1; i <= n; ++i) dfs(i,i,-1); 84 for(int i = 2; i <= n; ++i){ 85 for(int j = e[i].size()-1; j >= 0; --j){ 86 if(e[i][j] == 1) continue; 87 ans = max(ans,MST - g[i][e[i][j]] + dp[i][e[i][j]]); 88 } 89 } 90 printf("%.2f\n",ans*m); 91 } 92 return 0; 93 }
HDU 4756 Install Air Conditioning
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/crackpotisback/p/4044440.html