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

贴个代码给大家参考,如有不对的地方欢迎指教

Posted by smallsunny at 2011-11-19 19:18:45 on Problem 2987
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN = 5000;
const int MAXM = 100000;
const int inf = 0x3fffffff;
struct node
{
    int from, to, next, w;
}edge[MAXM+10];
int box[MAXN+10], ecnt;
int gap[MAXN+10], d[MAXN+10], cur[MAXN+10], pre[MAXN+10];
bool visit[MAXN+10];
int s, t, N;
int num;
int n, m;
void _make_map(int from, int to, int w)
{
    edge[ecnt].from = from;
    edge[ecnt].to = to;
    edge[ecnt].w = w;
    edge[ecnt].next = box[from];
    box[from] = ecnt++;
}
void make_map(int from, int to, int w)
{
    _make_map(from, to, w);
    _make_map(to, from, 0);
}
long long SAP()
{
    memset(gap, 0, sizeof(gap));
    memset(d, 0, sizeof(d));
    for(int i = 0; i < N; i++) cur[i] = box[i];
    long long maxflow = 0;
    int aug = inf, u = s;
    gap[0] = N;
    while(d[s] <= N)
    {
        loop:
        for(int i = cur[u]; i+1; i = edge[i].next)
        {
            if(edge[i].w && d[u]==d[edge[i].to]+1)
            {
                int v = edge[i].to;
                aug = min(aug, edge[i].w);
                cur[u] = i;
                pre[v] = i;
                u = v;
                if(u == t)
                {
                    maxflow += aug;
                    for(int i = pre[u]; ; i = pre[edge[i].from])
                    {
                        edge[i].w -= aug;
                        edge[i^1].w += aug;
                        if(edge[i].from == s) break;
                    }
                    aug = inf;
                    u = s;
                }
                goto loop;
            }
        }
        int mind = N;
        for(int i = box[u]; i+1; i = edge[i].next)
        {
            if(edge[i].w && d[edge[i].to]<mind)
            {
                mind = d[edge[i].to];
                cur[u] = i;
            }
        }
        if(--gap[d[u]] <= 0) break;
        gap[d[u] = mind+1]++;
        if(u != s) u = edge[pre[u]].from;
    }
    return maxflow;
}
void dfs(int x)
{
    if(x == t) return;
    if(x != s) num++;
    visit[x] = true;
    for(int i = box[x]; i+1; i = edge[i].next)
    {
        if(!visit[edge[i].to] && edge[i].w)
        {
            visit[edge[i].to] = true;
            dfs(edge[i].to);
        }
    }
}
int main()
{
    //freopen("in.txt", "r", stdin);
    scanf("%d%d", &n, &m);
    memset(box, -1, sizeof(box));
    ecnt = 0;
    s = 0;
    t = n+1;
    N = t+1;
    long long sum = 0;
    for(int i = 1; i <= n; i++)
    {
        int w;
        scanf("%d", &w);
        if(w > 0)
        {
            make_map(s, i, w);
            sum += w;
        }
        else make_map(i, t, -w);
    }
    for(int i = 1; i <= m; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        make_map(u, v, inf);
    }
    long long mflow = SAP();
    memset(visit, false, sizeof(visit));
    num = 0;
    dfs(s);
    printf("%d %lld\n", num, sum-mflow);
    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