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 Ioencgc at 2023-08-07 23:26:59 on Problem 3469
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define ll long long

const int MAX_V = 20002;
struct edge
{
    int to, cap, rev;
    edge (int t, int c, int r):
        to(t), cap(c), rev(r){}
};
vector<edge> G[MAX_V];
int level[MAX_V], iter[MAX_V];

inline void add_edge(int from, int to, int cap)
{
    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));
}

void bfs(int s)
{
    queue<int> que;
    que.push(s);
    memset(level, -1, sizeof(level));
    level[s] = 0;
    for (int v; !que.empty(); que.pop())
    {
        v = que.front();
        for (int i = 0; i < G[v].size(); ++i)
        {
            edge& e = G[v][i];
            if (e.cap > 0 && level[e.to] < 0)
            {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v, int t, int f)
{
    if (v == t)
        return f;
    for (int& i = iter[v]; i < G[v].size(); ++i)
    {
        edge& e = G[v][i];
        if (e.cap > 0 && level[v] < level[e.to])
        {
            int d = dfs(e.to, t, min(e.cap, f));
            if (d > 0)
            {
                e.cap -= d, G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t)
{
    int res = 0;
    for (int f; ; )
    {
        bfs(s);
        if (level[t] < 0)
            return res;
        memset(iter, 0, sizeof(iter));
        while (f = dfs(s, t, 1 << 30))
            res += f;
    }
}

int main()
{
    int n, m, s = 0, t;
    scanf("%d%d", &n, &m);
    t = n + 1;
    for (int a, b, i = 1; i <= n; ++i)
    {
        scanf("%d%d", &a, &b);
        add_edge(i, t, a);
        add_edge(s, i, b);
    }
    for (int a, b, w; m--; )
    {
        scanf("%d%d%d", &a, &b, &w);
        add_edge(a, b, w);
        add_edge(b, a, w);
    }
    printf("%d\n", max_flow(s, t));
    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