hdu 3371 有毒的卡时间题目

同样的代码 每次交的结果都不一样

#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<iostream>
using namespace std; struct Node { int from; int to; int w; }Edges[25500]; int father[550],ans; int find(int x) { int root = x; while(root != father[root]) { root = father[root]; } int z; while(x != root) { z = father[x]; father[x] = root; x = z; } return root; } bool cmp(Node a,Node b) { return a.w < b.w; } //int cmp(const void * a, const void * b) //{ // return ((Node *)a)->w - ((Node *)b)->w; //} bool Kruskal(int N,int M,int k) { sort(Edges,Edges+M,cmp); // qsort(Edges,M,sizeof(Node),cmp); int x,y; for(int i = 0; i < M; i++) { x = find(Edges[i].from); y = find(Edges[i].to); if(x != y) { father[y] = x; k++; ans += Edges[i].w; if(k == N-1) return true; } } if(k == N-1) return true; else return false; } int main() { int T,k,t,a,b,N,M,K; scanf("%d",&T); while(T--) { scanf("%d%d%d",&N,&M,&K); for(int i = 1; i <= N; i++) father[i] = i; for(int i = 0; i < M; i++) { scanf("%d%d%d",&Edges[i].from,&Edges[i].to,&Edges[i].w); } k = 0,ans = 0; for(int i = 0; i < K; i++) { scanf("%d%d",&t,&a); a = find(a); t--; while(t--) { scanf("%d",&b); b = find(b); if(a != b) { father[b] = a; k++; } } } if(Kruskal(N,M,k)) printf("%d\n",ans); else printf("-1\n"); } return 0; }
posted @ 2016-08-24 20:13  猪突猛进!!!  阅读(168)  评论(0编辑  收藏  举报