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

大侠解答下,通过了Sample output,为什么还是WA呢?

Posted by baigege111 at 2014-11-10 19:51:38 on Problem 3723
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAX_R = 50000;
int N, M, R;
int x[MAX_R], y[MAX_R], d[MAX_R];


static int *id, *sz;
void UFinit(int N) {
     int i;
     id = (int *)malloc(N * sizeof(int));
     sz = (int *)malloc(N * sizeof(int));
     for(i = 0; i < N; ++i) {
           id[i] = i; sz[i] = 1;
     }
}
static int find(int x) {
       int i = x;
       while (i != id[i]) i = id[i];
       return i;
}
int UFfind(int p, int q) {
    return (find(p) == find(q));
}
void UFunion(int p, int q) {
     int i = find(p), j = find(q);
     if (i == j) return;
     if (sz[i] < sz[j]) {
               id[i] = j; sz[j] += sz[i];
     }
     else {
               id [j] = i; sz[i] +=sz[j];
     }
}


const int MAX_E = 50000;
struct edge { int u, v, cost; 
edge(int uu = 0, int vv = 0, int cc = 20000) 
{ u = uu; v = vv; cost == cc;}
};
bool comp(const edge &e1, const edge &e2) {
     return e1.cost < e2.cost;
}
edge es[MAX_E];
int V, E;
int kruskal() {
    sort(es, es + E, comp);
    UFinit(V);
    int res = 0;
    for(int i = 0; i < E && i < V - 1; ++i) {
            edge e = es[i];
            if (!UFfind(e.u, e.v)) {
                           UFunion(e.u, e.v);
                           res += e.cost;
            }
    }
    free(id), free(sz);
    return res;
}

void solve() {
     V = N + M;
     E = R;
     for (int i = 0; i < R; ++i) {
         edge e;
         e.u = x[i];
         e.v = N + y[i];
         e.cost = -d[i];
         es[i] = e;
     }
     printf("%d\n", 10000 * (N + M) + kruskal());
} 

int main(int argc, char *argv[])
{
    int T;
    scanf("%d", &T);
    for (int t = 0; t < T; ++t) {
        scanf("%d%d%d", &N, &M, &R);
        for(int r = 0; r < R; ++r) {
                scanf("%d%d%d", &x[r], &y[r], &d[r]);
        }
         
        solve();
    }
    //system("PAUSE");
    return EXIT_SUCCESS;
}

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