Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么数组下标从0开始就WA 从1开始就A。。

Posted by Mitrenick at 2014-04-19 11:47:08 on Problem 1287
这是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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator