POJ 3204 最大流

题目大意是这样,找这样一种边的个数,就是增加该边的容量,可以使得最大流变大

那么首先我们求一遍最大流,如果某条边符合我们的要求,那么这条边必然是满流的,否则增加容量也没有用,然后假设端点为u->v,那么必然存在从源点到u的不饱和的路径,也就是路径上的每条边都是不满的,同样从v到汇点也应该有这样的路径,也只有这样才会在该边增加容量后形成一条增广路。

那么我们搞定这个问题的方法就是DFS了,从源点出发,沿着那些不满的边进行DFS,这样遍历到的点均是可以与源点有一条不饱和的路径的。

同理从汇点出发,进行逆向的DFS,这时最好先建个逆图。

最后枚举边即可。


#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 1111
#define MAXM 55555
#define INF 10000007
using namespace std;
struct node
{
    int v;    // vtex
    int c;    // cacity
    int f;   // current f in this arc
    int next, r;
}edge[MAXM];
int dist[MAXN], nm[MAXN], src, des, n;
int head[MAXN], e;
void add(int x, int y, int c)
{
    edge[e].v = y;
    edge[e].c = c;
    edge[e].f = 0;
    edge[e].r = e + 1;
    edge[e].next = head[x];
    head[x] = e++;
    edge[e].v = x;
    edge[e].c = 0;
    edge[e].f = 0;
    edge[e].r = e - 1;
    edge[e].next = head[y];
    head[y] = e++;
}
void rev_BFS()
{
    int Q[MAXN], h = 0, t = 0;
    for(int i = 1; i <= n; ++i)
    {
        dist[i] = MAXN;
        nm[i] = 0;
    }
    Q[t++] = des;
    dist[des] = 0;
    nm[0] = 1;
    while(h != t)
    {
        int v = Q[h++];
        for(int i = head[v]; i != -1; i = edge[i].next)
        {
            if(edge[edge[i].r].c == 0 || dist[edge[i].v] < MAXN)continue;
            dist[edge[i].v] = dist[v] + 1;
            ++nm[dist[edge[i].v]];
            Q[t++] = edge[i].v;
        }
    }
}
void init()
{
    e = 0;
    memset(head, -1, sizeof(head));
}
int maxflow()
{
    rev_BFS();
    int u;
    int total = 0;
    int cur[MAXN], rpath[MAXN];
    for(int i = 1; i <= n; ++i)cur[i] = head[i];
    u = src;
    while(dist[src] < n)
    {
        if(u == des)     // find an augmenting path
        {
            int tf = INF;
            for(int i = src; i != des; i = edge[cur[i]].v)
                tf = min(tf, edge[cur[i]].c);
            for(int i = src; i != des; i = edge[cur[i]].v)
            {
                edge[cur[i]].c -= tf;
                edge[edge[cur[i]].r].c += tf;
                edge[cur[i]].f += tf;
                edge[edge[cur[i]].r].f -= tf;
            }
            total += tf;
            u = src;
        }
        int i;
        for(i = cur[u]; i != -1; i = edge[i].next)
            if(edge[i].c > 0 && dist[u] == dist[edge[i].v] + 1)break;
        if(i != -1)     // find an admissible arc, then Advance
        {
            cur[u] = i;
            rpath[edge[i].v] = edge[i].r;
            u = edge[i].v;
        }
        else        // no admissible arc, then relabel this vtex
        {
            if(0 == (--nm[dist[u]]))break;    // GAP cut, Important!
            cur[u] = head[u];
            int mindist = n;
            for(int j = head[u]; j != -1; j = edge[j].next)
                if(edge[j].c > 0)mindist = min(mindist, dist[edge[j].v]);
            dist[u] = mindist + 1;
            ++nm[dist[u]];
            if(u != src)
                u = edge[rpath[u]].v;    // Backtrack
        }
    }
    return total;
}
int nt, m;
vector<int>g[MAXN], rg[MAXN];
int uu[MAXM], vv[MAXM];
int color1[MAXN], color2[MAXN];
void dfs1(int u)
{
    color1[u] = 1;
    int size = g[u].size();
    for(int i = 0; i < size; i++)
    {
        int v = g[u][i];
        if(!color1[v]) dfs1(v);
    }
}
void dfs2(int u)
{
    color2[u] = 1;
    int size = rg[u].size();
    for(int i = 0; i < size; i++)
    {
        int v = rg[u][i];
        if(!color2[v]) dfs2(v);
    }
}
int main()
{
    int u, v, w;
    scanf("%d%d", &nt, &m);
    init();
    src = 1;
    des = nt;
    n = nt;
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        uu[i] = u + 1, vv[i] = v + 1;
        add(uu[i], vv[i], w);
    }
    maxflow();
    for(int i = 0; i < MAXN; i++)
        g[i].clear(), rg[i].clear();
    for(int i = 0; i < e; i += 2)
        if(edge[i].c > 0)
        {
            int id = i / 2 + 1;
            g[uu[id]].push_back(vv[id]);
            rg[vv[id]].push_back(uu[id]);
        }
    dfs1(src), dfs2(des);
    int ans = 0;
    for(int i = 0; i < e; i += 2)
        if(edge[i].c == 0)
        {
            int id = i / 2 + 1;
            if(color1[uu[id]] && color2[vv[id]]) ans++;
        }
    printf("%d\n", ans);
    return 0;
}


你可能感兴趣的:(c,Path)