POJ1273 Drainage Ditches 【最大流Dinic】

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 56870 Accepted: 21863

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

Source

题意:给定n和m,其中n表示水沟的条数,m表示汇点。然后给出n条水沟的详细情况s、t、c表示从节点s到t的容量为c。其中1为源点,求源点到汇点的最大流。

题解:Dinic算法流程,先对原图用BFS进行分层处理,即每一个点相对于源点的最近位置,一直处理到汇点为止。然后从源点开始进行深搜(用栈维护)确保每次都是按照标号增1的顺序搜索下去,如果不能搜索下去则回溯,直到找到一条增广路,然后路径上的每一段都减少此路上的瓶颈值,然后继续深搜,若搜不出增广路则重新分层,接着深搜下去,直到最终无法分层才得到最大流值。

#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 202
#define inf 0x7fffffff
using std::queue;

int G[maxn][maxn], Layer[maxn];
int Sta[maxn];
bool vis[maxn];

bool countLayer(int m) {
    queue<int> Q; Q.push(1);
    memset(Layer, 0, sizeof(int) * (m + 1));
    int i, now;
    Layer[1] = 1;
    while(!Q.empty()) {
        now = Q.front(); Q.pop();
        for(i = 1; i <= m; ++i) {
            if(G[now][i] && !Layer[i]) {
                Layer[i] = Layer[now] + 1;
                if(m == i) return true;
                else Q.push(i);
            }
        }
    }
    return false;
}

int Dinic(int m) {
    int minCut, minCutPos, maxFlow = 0;
    int s, t, i, id = 0, now;
    while(countLayer(m)) {
        memset(vis, 0, sizeof(bool) * (m + 1));
        vis[1] = 1; Sta[id++] = 1;
        while(id) {
            //栈中需要存放路径,所以不出栈
            now = Sta[id - 1];
            if(now == m) {
                minCut = inf;
                for(i = 1; i < id; ++i) {
                    s = Sta[i - 1];
                    t = Sta[i];
                    if(G[s][t] && G[s][t] < minCut) {
                        minCut = G[s][t];
                        minCutPos = s;
                    }
                }
                //增广,添加取消流
                maxFlow += minCut;
                for(i = 1; i < id; ++i) {
                    s = Sta[i - 1]; t = Sta[i];
                    G[s][t] -= minCut;
                    G[t][s] += minCut;
                }
                //出栈到最小割位置
                while(id && Sta[id - 1] != minCutPos) {
                    vis[Sta[--id]] = 0;
                }
            } else {
                for(i = 1; i <= m; ++i) {
                    if(G[now][i] && !vis[i] && Layer[now] + 1 == Layer[i]) {
                        Sta[id++] = i; vis[i] = 1;
                        break;
                    }
                }
                if(i > m) --id;
            }
        }
    }
    return maxFlow;
}

int main() {
    //freopen("stdin.txt", "r", stdin);
    int n, m, i, s, t, c;
    while(scanf("%d%d", &n, &m) == 2) {
        memset(G, 0, sizeof(G));
        for(i = 0; i < n; ++i) {
            scanf("%d%d%d", &s, &t, &c);
            G[s][t] += c;
        }
        printf("%d\n", Dinic(m));
    }
    return 0;
}

2015.04.20

#include <stdio.h>
#include <string.h>

const int maxn = 210;
const int maxm = 210;
const int inf = 0x3f3f3f3f;
int N, M;
int head[maxn], id;
struct Node {
    int u, v, c, next;
} E[maxm<<1];

 // 参数:顶点个数(若编号0不用则传入最大编号+1),源点,汇点
int dep[maxn], ps[maxn], cur[maxn];
int Dinic(int n, int s, int t) {
    int tr, res = 0;
    int i, j, k, f, r, top;
    while(true) {
        memset(dep, -1, n * sizeof(int));
        for(f = dep[ps[0] = s] = 0, r = 1; f != r; )
            for(i = ps[f++], j = head[i]; j != -1; j = E[j].next) {
                if(E[j].c && -1 == dep[k=E[j].v]) {
                    dep[k] = dep[i] + 1; ps[r++] = k;
                    if(k == t) {
                        f = r; break;
                    }
                }
            }
        if(-1 == dep[t]) break;

        memcpy(cur, head, n * sizeof(int));
        for(i = s, top = 0; ; ) {
            if(i == t) {
                for(k = 0, tr = inf; k < top; ++k)
                    if(E[ps[k]].c < tr) tr = E[ps[f=k]].c;
                for(k = 0; k < top; ++k)
                    E[ps[k]].c -= tr, E[ps[k]^1].c += tr;
                res += tr; i = E[ps[top = f]].u;
            }
            for(j = cur[i]; cur[i] != -1;j = cur[i] = E[cur[i]].next)
                if(E[j].c && dep[i] + 1 == dep[E[j].v]) break;
            if(cur[i] != -1) {
                ps[top++] = cur[i];
                i = E[cur[i]].v;
            } else {
                if(0 == top) break;
                dep[i] = -1; i = E[ps[--top]].u;
            }
        }
    }
    return res;
}

void init()
{
    memset(head, -1, sizeof(head));
    id = 0;
}

void addEdge(int u, int v, int c)
{
    E[id].u = u;
    E[id].v = v;
    E[id].c = c;
    E[id].next = head[u];
    head[u] = id++;

    E[id].u = v;
    E[id].v = u;
    E[id].c = 0;
    E[id].next = head[v];
    head[v] = id++;
}

void getMap()
{
    int u, v, c, i;
    while (N--) {
        scanf("%d%d%d", &u, &v, &c);
        addEdge(u, v, c);
    }
}

int solve()
{
    printf("%d\n", Dinic(M + 1, 1, M));
}

int main()
{
    while (~scanf("%d%d", &N, &M)) {
        init();
        getMap();
        solve();
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值