标签:des blog http io os ar for div sp
。。
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; } #define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=20005; int p[N], n, m, ans; struct ED { int x, y, w; }e[N]; bool cmp(const ED &a, const ED &b) { return a.w>b.w; } const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); } int main() { read(n); read(m); for1(i, 1, m) read(e[i].x), read(e[i].y), read(e[i].w); sort(e+1, e+1+m, cmp); int tot=0; for1(i, 1, n) p[i]=i; for1(i, 1, m) { int fx=ifind(e[i].x), fy=ifind(e[i].y); if(fx!=fy) { p[fx]=fy; ans+=e[i].w; ++tot; } } if(tot<n-1) puts("-1"); else print(ans); return 0; }
【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)
标签:des blog http io os ar for div sp
原文地址:http://www.cnblogs.com/iwtwiioi/p/3979067.html