Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
求大神看一下,总是runtime error,然而并不知道错哪里了~急急急#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> struct EDG { int u, v, value; }; const int maxn = 103; int N; int ans = 0; int par[maxn]; int Rank[maxn]; EDG edg[maxn]; int Find(int u) { if(u = par[u]) return u; return Find(par[u]); } void Union(EDG e) { int x = Find(e.u); int y = Find(e.v); if(x == y) return ; if(Rank[x] < Rank[y]) par[x] = y; else { if(Rank[x] == Rank[y]) Rank[y]++; par[y] = x; } ans += e.value; } bool comp(EDG a, EDG b) { return a.value < b.value; } void init() { for(int i = 1; i <= N; i++) { Rank[i] = 0;par[i] = i; } } using namespace std; int main(void) { int cost = 0; int cnt = 0; while(scanf("%d", &N) != EOF) { for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { scanf("%d", &cost); if(i > j) { edg[cnt].u = i,edg[cnt].v = j, edg[cnt].value = cost; cnt++; } } } init(); ans = 0; sort(edg, edg+cnt, comp); for(int i = 0; i < cnt; i++) { Union(edg[i]); } cout<<ans<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator