标签:des style blog os io 数据 for 2014
2 2 10 10 20 20 3 1 1 2 2 1000 1000
1414.2 oh!
总共最多有100个节点,那么边最大100*100/2,把所有 符合题意 的边都求出来,然后就是最小生成树了。
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <stack> #define lson o<<1, l, m #define rson o<<1|1, m+1, r using namespace std; typedef long long LL; const int maxn = 105; const int mod = 1000000007; int n, t, fa[105]; double q; struct C1{ int st, en; double v; }ed[6000]; struct C2 { int x, y; }in[105]; double getdis(double x1, double y1, double x2, double y2) { return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ); } bool cmp (C1 x, C1 y) { return x.v < y.v; } int Find (int x) { return x == fa[x] ? x : x = Find(fa[x]); } int main() { scanf("%d", &t); while(t--) { int n; scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d%d", &in[i].x, &in[i].y); int k = 0; for(int i = 0; i < n-1; i++) for(int j = i+1; j < n; j++) { double tmp = getdis(in[i].x, in[i].y, in[j].x, in[j].y); if(tmp >= 10 && tmp <= 1000) { ed[k].v = tmp*100; ed[k].st = i; ed[k++].en = j; } } sort(ed, ed+k, cmp); for(int i = 0; i <= 100; i++) fa[i] = i; double sum = 0; int cnt = 0; for(int i = 0; i <k; i++) { int pres = Find(ed[i].st), pree = Find(ed[i].en); if(pres != pree) { fa[pres] = pree; sum += ed[i].v; cnt++; if(cnt == n-1) break; } } if(cnt == n-1) printf("%.1lf\n", sum); else printf("oh!\n"); } return 0; }
HDU 1875 畅通工程再续 (最小生成树 水),布布扣,bubuko.com
标签:des style blog os io 数据 for 2014
原文地址:http://blog.csdn.net/u013923947/article/details/38462971