标签:style blog http java color os
2 4 1 1 20 1 2 30 200 2 80 200 1 100 3 1 1 20 1 2 30 2 2 40
65.00 70.00
解题:此题跟次小生成树没多大关系,只是涉及到最小生成树的遍历问题。解题思路就是先求出最小生成树,存储这棵树,然后再在这棵树上进行去边操作,此边的 两个端点城市的人口和 去除以 最小生成树的值减去此边的值 后的商,求这个商最大可能是多少。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long 10 #define INF 0x3f3f3f 11 using namespace std; 12 const int maxv = 1010; 13 const int maxe = 500*1010; 14 struct arc{ 15 int u,v; 16 double w; 17 }; 18 int uf[maxv]; 19 vector<int>g[maxv]; 20 arc e[maxe],tree[maxe]; 21 int n,px[maxv],py[maxv],val[maxv]; 22 int maxVal,es,ads; 23 double Minst; 24 bool vis[maxv]; 25 bool cmp(const arc &x,const arc &y){ 26 return x.w < y.w; 27 } 28 void dfs(int u){ 29 vis[u] = true; 30 if(val[u] > maxVal) maxVal = val[u]; 31 for(int i = 0; i < g[u].size(); i++){ 32 if(!vis[g[u][i]]) dfs(g[u][i]); 33 } 34 } 35 int findF(int x){ 36 if(x != uf[x]) 37 uf[x] = findF(uf[x]); 38 return uf[x]; 39 } 40 double dis(int i,int j){ 41 double temp = (px[i]-px[j])*(px[i]-px[j])+(py[i]-py[j])*(py[i]-py[j]); 42 return sqrt(temp); 43 } 44 void kruskal(){ 45 for(int i = 0; i < es; i++){ 46 int x = findF(e[i].u); 47 int y = findF(e[i].v); 48 if(x != y){ 49 Minst += e[i].w; 50 uf[x] = y; 51 g[e[i].u].push_back(e[i].v); 52 g[e[i].v].push_back(e[i].u); 53 tree[ads++] = e[i]; 54 } 55 } 56 } 57 int main(){ 58 int t,i,j; 59 scanf("%d",&t); 60 while(t--){ 61 scanf("%d",&n); 62 for(i = 1; i <= n; i++){ 63 scanf("%d%d%d",px+i,py+i,val+i); 64 g[i].clear(); 65 uf[i] = i; 66 } 67 ads = es = 0; 68 Minst = 0; 69 for(i = 1; i <= n; i++) 70 for(j = i+1; j <= n; j++){ 71 e[es++] = (arc){i,j,dis(i,j)}; 72 } 73 sort(e,e+es,cmp); 74 kruskal(); 75 double ans = 0,temp; 76 for(i = 0; i < ads; i++){ 77 int u = tree[i].u; 78 int v = tree[i].v; 79 memset(vis,false,sizeof(vis)); 80 vis[v] = true; 81 temp = maxVal = 0; 82 dfs(u); 83 temp += maxVal; 84 memset(vis,false,sizeof(vis)); 85 maxVal = 0; 86 vis[u] = true; 87 dfs(v); 88 temp += maxVal; 89 ans = max(ans,temp/(Minst-tree[i].w)); 90 } 91 printf("%.2f\n",ans); 92 } 93 return 0; 94 }
图论trainning-part-1 H. Qin Shi Huang's National Road System,布布扣,bubuko.com
图论trainning-part-1 H. Qin Shi Huang's National Road System
标签:style blog http java color os
原文地址:http://www.cnblogs.com/crackpotisback/p/3862771.html