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

C++提交AC,G++提交WA,求解,附代码,请指教

Posted by hjp at 2013-02-04 01:30:00 on Problem 1273
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

//#define DEBUG

typedef long long captype;

typedef struct Vertex_ {
    int id;
    captype capacity;
    struct Vertex_* next;
    struct Vertex_* ref;
} Vertex;

#define N 500
Vertex poolVertices[N+10];
int indexPool = 0;
void initPool() {
    indexPool = 0;
}
Vertex* getVertex(int id, captype capacity, Vertex* next, Vertex* ref) {
    poolVertices[indexPool].next = next;
    poolVertices[indexPool].ref = ref;
    poolVertices[indexPool].capacity = capacity;
    poolVertices[indexPool].id = id;
    return poolVertices + indexPool++;
}

Vertex residualGraph[N+10];
void initGraph(Vertex* g, int n) {
    for (int i = 1; i <= n; ++i) {
        g[i].next = NULL;
    }
}
void addEdge(Vertex* g, int u, int v, captype c) {
    bool edgeBuilt = false;
    for (Vertex *p = g[u].next; p != NULL; p = p->next) {
        if (p->id == v) {
            p->capacity += c;
            edgeBuilt = true;
            break;
        }
    }
    if (!edgeBuilt) {
        Vertex* p = getVertex(v, c, g[u].next, NULL);
        Vertex* p2 = getVertex(u, 0, g[v].next, p);
        p->ref = p2;
        g[u].next = p;
        g[v].next = p2;
    }
}

#define INF 1e18
bool visited[N + 10];
captype augment0(Vertex* g, int s, int t, captype curBottleNeck) {
    visited[s] = true;
    bool found = false;
    Vertex* v = NULL;
    captype bottleNeck = 0;
    for (Vertex* tra = g[s].next; tra != NULL && !found; tra = tra->next) {
        if (tra->capacity <= 0 || visited[tra->id]) {
            continue;
        }
        if (tra->id == t) {
            bottleNeck = min(tra->capacity, curBottleNeck);
            found = true;
            v = tra;
        } else {
            bottleNeck = min(curBottleNeck, tra->capacity);
            bottleNeck = augment0(g, tra->id, t, bottleNeck);
            if (bottleNeck > 0) {
                found = true;
                v = tra;
            }
        }
    }

    if (found) {
        v->capacity -= bottleNeck;
        v->ref->capacity += bottleNeck;
#ifdef DEBUG
        printf("%d ", v->id);
#endif
    }
    return bottleNeck;
}       
captype augment(Vertex* g, int s, int t, int n) {
    captype totflow = 0;
    captype augmentedFlow;
    while (augmentedFlow != 0) {
        memset(visited, false, sizeof(visited));
        augmentedFlow = augment0(g, s, t, INF);
        totflow += augmentedFlow;
#ifdef DEBUG
        printf("augment: %I64d\n", augmentedFlow);
#endif
    }
    return totflow;
}

int main(int argc, char *argv[]) {
    int ne, nv, u, v;
    captype c;
    while (scanf("%d%d", &ne, &nv) != EOF) {
        initPool();
        initGraph(residualGraph, nv);
        for (int i = 0; i < ne; ++i) {
            //scanf("%d%d%I64d", &u, &v, &c);
            cin >> u >> v >> c;
            addEdge(residualGraph, u, v, c);
        }

        captype res = augment(residualGraph, 1, nv, nv);
        //printf("%I64d\n", res);
        cout << res << endl;
    }
    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