POJ 1273 Drainage Ditches 网络流 模板题

题意:直接看吧,没啥难度。

思路:模板题,检验模板的好题,但是好像数据有点弱。

代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int N,M;
int u,v,c;

struct edge{
    int from,to;
    int cap,flow;
    edge(int u =0,int v =0,int c=0,int f=0):from(u),to(v),cap(c),flow(f){}
};

struct ISAP{
    static const int INF = 0x3f3f3f3f;
    static const int MAX = 100010;//2倍的边的大小
    int head[MAX];//每个节点对应链表的开始位置
    int next[MAX];//链表的下一个节点在edges数组的位置
    int tot;//edges数组的大小
    edge edges[MAX];//储存边的数组
    int que[MAX],front,tail;//队列,保存节点
    int d[MAX];//距离标号
    bool vis[MAX];//访问标记
    int num[MAX];//gap优化
    int pre[MAX];//增广路中,节点X的前面一个弧的标号
    int cur[MAX];//对于每个节点的,处理的当前弧。
    int s,t,n;//s源点标号,t汇点标号,n节点总数
    void init(int n){
        this->n = n;//注意此处的下标问题
        memset(head,-1,sizeof(int)*(n+1));
        tot = 0;//
    }
    void addedge(int from,int to, int cap){
        edges[tot] = edge(from,to,cap,0);
        next[tot] = head[from], head[from] = tot++;
        edges[tot] = edge(to,from,0,0);
        next[tot] = head[to],head[to] = tot++;
    }
    void bfs(){
        memset(vis,0,sizeof(vis));
        front = tail = 0;
        d[t] = 0;
        vis[t] = true;
        que[tail++] = t;
        while(front < tail){
            int u = que[front++];
            for(int v = head[u]; v != -1; v = next[v]){
                edge & e = edges[v^1];
                if(e.cap > e.flow && !vis[e.from]){//对处于残余网络中的弧且没访问过的节点处理
                    d[e.from] = d[u] + 1;
                    vis[e.from] = true;
                    que[tail++] = e.from;
                }
            }
        }
    }
    int augment(){
        int x = t,a = INF;
        while(x != s){
            edge& e = edges[pre[x]];
            a = min(a,e.cap - e.flow);
            x = e.from;
        }

        x = t;
        while(x != s){
            edges[pre[x]].flow += a;
            edges[pre[x]^1].flow -= a;
            x = edges[pre[x]].from;
        }
        return a;
    }
    int maxflow(int s, int t){
        this->s = s, this->t = t;
        memset(num,0,sizeof(num));
        int flow = 0;
        bfs();
        for(int i = 1; i <= n; ++i){//注意此处的下标问题
            num[d[i]]++;
            cur[i] = head[i];
        }
        int x = s;
        while(d[s] < n){
            if(x == t){
                flow += augment();
                x = s;
            }
            bool ok = false;
            for(int &v = cur[x]; v != -1; v = next[v]){
                edge& e = edges[v];
                if(e.cap > e.flow && d[x] == d[e.to] + 1){
                    ok = true;
                    pre[x = e.to] = v;
                    break;
                }
            }
            if(!ok){
                int m = n - 1;
                for(int v = head[x]; v != -1; v = next[v]){
                    edge & e = edges[v];
                    if(e.cap > e.flow) m = min(m,d[e.to]);
                }
                if(--num[d[x]] == 0) break;
                num[d[x]=m+1]++;
                cur[x] = head[x];
                if(x != s) x = edges[pre[x]].from;
            }
        }
        return flow;
    }
} solver;


int main(void)
{
    //freopen("input.txt","r",stdin);
    while(scanf("%d %d", &N, &M) != EOF){
        solver.init(M);
        int s = 1, t = M;
        for(int i = 0; i < N; ++i){
            int u,v,c;
            scanf("%d %d %d", &u, &v,&c);
            solver.addedge(u,v,c);
        }
        printf("%d\n",solver.maxflow(s,t));
       // for(int i = 0 ; i < solver.tot; ++i){
        //    edge & e = solver.edges[i];
       //     printf("from: %d to: %d cap: %d flow:%d\n",e.from,e.to,e.cap,e.flow);
      //  }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值