标签:des style blog color os io 数据 for
3 3 0 1 1 1 2 5 0 2 2 4 3 0 1 1 1 2 3 0 2 4
5 -1
#include<iostream> #include<cstdio> #include<cstring> #include <cstdlib> #include <math.h> #include <algorithm> #define INF 0x3f3f3f3f const int N = 100010; using namespace std; struct node { int u,v,w; } g[N]; int num = 0,sum = 0,zong = 0,n,m,father[N]; int cmp(const void *a,const void *b) { struct node *X,*Y; X = (struct node *)a; Y = (struct node *)b; return X->w - Y->w; } int findx(int r) { while(r != father[r]) { r = father[r]; } int i = r,j; while(father[i] != r) { j = father[i]; father[i] = r; i = j; } return r; } void Kruskal() { for(int i = 0; i < m; i++) { int uu = findx(g[i].u); int vv = findx(g[i].v); if(uu!=vv) { num ++; sum += g[i].w; father[uu] = vv; } if(num == n-1) break; } } void init() { zong = 0,num = 0,sum = 0; for(int i = 0; i <=n; i++) father[i] = i; } int main() { while(~scanf("%d%d",&n,&m)) { init(); for(int i = 0; i < m; i++) { scanf("%d%d%d",&g[i].u,&g[i].v,&g[i].w); zong += g[i].w; } qsort(g,m,sizeof(g[0]),cmp); Kruskal(); (num==n-1)?printf("%d\n",zong-sum):puts("-1"); } }
人活着系列之Streetlights (克鲁斯卡尔),布布扣,bubuko.com
标签:des style blog color os io 数据 for
原文地址:http://blog.csdn.net/wjw0130/article/details/38371555