标签:
Description
Input
Output
Sample Input
9 1 5 0 0 3 2 4 5 5 1 0 4 5 2 1 2 5 3 3 1 3 9 7 1 2
Sample Output
1 6 3 7 4 9 5 7 8 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; const int maxn = 760; int n,m; int cnt = 0; int x[maxn],y[maxn]; struct Edge{ int u,v; long long c; bool operator < ( const Edge & a) const { return c < a.c; } }e[maxn*(maxn-1)/2 + 1000]; int fa[maxn]; void ini(){ for ( int i = 0;i < n + 5;i++) fa[i] = i; } int fnd( int x) { return x == fa[x] ? x : (fa[x] = fnd(fa[x])); } int kruskal() { ini(); sort(e,e + cnt); long long res = 0; int ct = 0; for ( int i = 0;i < cnt;i++) { if (ct == n - 1) break ; int u = e[i].u,v = e[i].v; u = fnd(u); v = fnd(v); if (u != v) { fa[u] = v; if (e[i].c != 0 ) { res += e[i].c; printf ( "%d %d\n" ,e[i].u,e[i].v); } ct++; } } return res; } void uni( int x, int y){ fa[fnd(x)] = fnd(y); } int main() { scanf ( "%d" ,&n); for ( int i = 1;i <= n;i++) scanf ( "%d%d" ,&x[i],&y[i]); for ( int i = 1;i <= n;i++) { for ( int j = 1;j <= n ;j++) { if ( i < j ) { e[cnt].u = i; e[cnt].v = j; e[cnt].c = (x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]); cnt++; } } } scanf ( "%d" ,&m); int a,b; for ( int i = 0;i < m ;i++){ scanf ( "%d%d" ,&a,&b); e[cnt].u = a; e[cnt].v = b; e[cnt].c = 0; cnt++; } kruskal(); return 0; } |
[2016-01-27][POJ][1751][B - Highways]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/5164821.html