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 |
为什么数组下标从0开始就WA 从1开始就A。。这是WA的代码。。 #include <cstdio> #include <algorithm> using namespace std; #define MAX 100000 int n,m; int u[MAX],v[MAX],w[MAX],r[MAX],p[MAX]; bool cmp(const int i,const int j) { return w[i] < w[j]; } int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } int kruskal(void) { int ans = 0; for(int i = 0;i < n;i ++) p[i] = i; for(int i = 0;i < m;i ++) r[i] = i; sort(r,r + m,cmp); for(int i = 0;i < m;i ++) { int e = r[i]; int x = find(u[e]); int y = find(v[e]); if(x != y) { ans += w[e]; p[x] = y; } } return ans; } int main(void) { while(scanf("%d",&n) && n) { scanf("%d",&m); for(int i = 0;i < m;i ++) { scanf("%d%d%d",&u[i],&v[i],&w[i]); } printf("%d\n",kruskal()); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator