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

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

Posted by Mitrenick at 2014-04-19 11:48:04 on Problem 1287
In Reply To:为什么数组下标从0开始就WA 从1开始就A。。 Posted by:Mitrenick at 2014-04-19 11:47:08
然后这样就A了。
有什么不同吗?

#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 = 1;i <= n;i ++)
        p[i] = i;
    for(int i = 1;i <= m;i ++)
        r[i] = i;
    sort(r + 1,r + m + 1,cmp);
    for(int i = 1;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) {
    //freopen("input.txt","r",stdin);
    while(scanf("%d",&n) == 1 && n) {
        scanf("%d",&m);
        for(int i = 1;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