ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ1273【网络流】

2019-11-23 17:52:11  阅读:309  来源: 互联网

标签:head POJ1273 cur int cnt 网络 water sink


                                                        Drainage Ditches  
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 91824   Accepted: 35588

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

USACO 93   思路:给出点之间的容量,求整个网络的最大流,最大流板子题,这里用的是ISAP模板。  
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;
const int maxnE = 1e6 + 7;
const int maxn  = 1e5 + 7;
const int maxnQ = 1e6 + 7;
const int inf   = 0x3f3f3f3f;

struct edge {
    int v;///弧尾
    int cap;///容量
    int nxt;///指向下一条从同一个弧头出发的弧
}e[maxnE];

int head[maxn],cnt;
int d[maxn],cur[maxn],pre[maxn],num[maxn];
int source,sink;///超级源、超级汇
int nv;///编号修改的上限
int n,m;

queue <int> q;

void add(int u, int v, int capacity) {
    e[cnt].v = v;
    e[cnt].cap = capacity;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
    //正向边

    e[cnt].v = u;
    e[cnt].cap = 0;
    e[cnt].nxt = head[v];
    head[v] = cnt++;
    //反向边
}

void rev_bfs() {///反向bfs
    memset(num, 0, sizeof(num));
    memset(d, -1, sizeof(d));
    d[sink] = 0;///超级汇直接标记
    num[0] = 1;
    q.push(sink);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        for(int i = head[u]; ~i; i = e[i].nxt) {
            int v = e[i].v;
            if(~d[v]) continue;///已经标过号
            d[v] = d[u] + 1;
            q.push(v);
            num[d[v]]++;
        }
    }
}

int ISAP() {
    memcpy(cur, head, sizeof(cur));///当前弧优化
    rev_bfs();
    int flow = 0, u = pre[source] = source;
    int i;
    while(d[sink] < nv) {///最长的一条链上,最大的下标是nv-1,如果大于等于nv说明已断层
        //printf("flow:%d\n",flow);
        if(u == sink) {///如果找到一条增广路,则沿着此条路修改flow
            int f = inf, neck;
            for(i = source; i != sink; i = e[cur[i]].v) {///修改流量
                if(f > e[cur[i]].cap) {
                    f = e[cur[i]].cap;///不断减少所需要的流量
                    neck = i;///记录回退点,不用回到起点再找
                }
            }
            for(i = source; i != sink; i = e[cur[i]].v) {///修改流量
                e[cur[i]].cap -= f;
                e[cur[i] ^ 1].cap += f;
            }
            flow += f;
            u = neck;///回退
        }
        for(i = cur[u]; ~i; i = e[i].nxt) {
            if(d[e[i].v] + 1 == d[u] && e[i].cap) break;
        }
        if(~i) {
            //如果存在可行的增广路
            cur[u] = i;
            pre[e[i].v] = u;
            u = e[i].v;
        } else {///否则回退,重新找增广路
            if(0 == (--num[d[u]])) break;
            int mind = nv;
            for(i = head[u]; ~i; i = e[i].nxt) {
                if(e[i].cap && mind > d[e[i].v]) {///寻找可以增广的最小下标
                    cur[u] = i;
                    mind = d[e[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];///回退
        }
    }
    return flow;
}

void init() {///初始化
    memset(head, -1, sizeof(head));
    cnt = 0;
}

void solve()
{
    int u,v,c;
    init();
    for(int i = 0; i < m; ++i) {
        scanf("%d %d %d",&u, &v, &c);
        add(u,v,c);
    }
    source = 1, sink = n, nv = sink + 1;
    printf("%d\n",ISAP());
}

int main()
{
    while(scanf("%d %d", &m, &n) != EOF) {
        solve();
    }
    return 0;
}

 

标签:head,POJ1273,cur,int,cnt,网络,water,sink
来源: https://www.cnblogs.com/orangeko/p/11918969.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有