标签:test namespace while limit help jpg nal other 0.00
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10874 Accepted Submission(s): 3846
1 #include <cstdio> 2 #include <cmath> 3 #include <vector> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 const int maxn = 1000 + 5, maxe = 1000 * 1000 / 2 + 5, INF = 0x3f3f3f3f; 9 int n, m, head[maxn]; 10 double Max[maxn][maxn]; 11 struct City { 12 int u, v, population; 13 }city[maxn]; 14 struct Edge{ 15 int u, v, population; 16 double w; 17 bool vis; 18 }edge[maxe]; 19 vector <int> G[maxn]; 20 21 bool cmp(const Edge &a, const Edge &b) { 22 return a.w < b.w; 23 } 24 25 int Find(int x) { 26 if(x == head[x]) return x; 27 else return head[x] = Find(head[x]); 28 } 29 30 double Distance(int i, int j) { 31 int x1 = city[i].u, y1 = city[i].v, 32 x2 = city[j].u, y2 = city[j].v; 33 return sqrt((double)(x2 - x1) * (x2 - x1) + (double)(y2 - y1) * (y2 - y1)); 34 } 35 36 double Kruskal() { 37 int cnt = 0; 38 double ans = 0; 39 sort(edge + 1, edge + m + 1, cmp); 40 for(int i = 1; i <= n; i ++) { 41 G[i].clear(); 42 G[i].push_back(i); 43 head[i] = i; 44 } 45 for(int i = 1; i <= m; i ++) { 46 int fx = Find(edge[i].u), fy = Find(edge[i].v); 47 if(cnt == n - 1) break; 48 if(fx != fy) { 49 edge[i].vis = true; 50 ans += edge[i].w; 51 cnt ++; 52 int len_fx = G[fx].size(), len_fy = G[fy].size(); 53 for(int j = 0; j < len_fx; j ++) { 54 for(int k = 0; k < len_fy; k ++) { 55 Max[G[fx][j]][G[fy][k]] = Max[G[fy][k]][G[fx][j]] = edge[i].w; 56 } 57 } 58 head[fx] = fy; 59 for(int j = 0; j < len_fx; j ++) { 60 G[fy].push_back(G[fx][j]); 61 } 62 } 63 } 64 if(cnt < n - 1) return INF; 65 return ans; 66 } 67 68 double Second_Kruskal(double MST) { 69 double ans = 0; 70 for(int i = 1; i <= m; i ++) { 71 ans = max(ans, edge[i].population / (MST - Max[edge[i].u][edge[i].v])); 72 } 73 return ans; 74 } 75 76 int main () { 77 int t; 78 scanf("%d", &t); 79 while(t --) { 80 m = 0; 81 scanf("%d", &n); 82 for(int i = 1; i <= n; i ++) { 83 scanf("%d %d %d", &city[i].u, &city[i].v, &city[i].population); 84 } 85 for(int i = 1; i <= n - 1; i ++) { 86 for(int j = i + 1; j <= n; j ++) { 87 edge[++m].u = i; 88 edge[m].v = j; 89 edge[m].vis = false; 90 edge[m].population = city[i].population + city[j].population; 91 edge[m].w = Distance(i, j); 92 } 93 } 94 double MST = Kruskal(); 95 double ans = Second_Kruskal(MST); 96 printf("%.2f\n", ans); 97 } 98 return 0; 99 }
HDU-4081.Qinshihuang'sNationalRoadSystem(次小生成树变种)
标签:test namespace while limit help jpg nal other 0.00
原文地址:https://www.cnblogs.com/bianjunting/p/10840140.html