POJ 1273 Drainage Ditches (最大流 - 裸题)

题目链接:http://poj.org/problem?id=1273
题意:给你m条边,n个点,(0<=m<=200, 2<=n <=200),每条边包含两个端点和其权值,求从点1到点n的最大流。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#define pi acos(-1)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int INF = 0x3f3f3f3f;
const LL ll_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 200 + 10;
const int mod = 1e9 + 7;

int cap[maxn][maxn];
int dis[maxn];
int s, t;
int n, m;

bool BFS()
{
    queue<int> que;
    memset(dis, -1, sizeof(dis));
    dis[s] = 0;
    que.push(s);
    while(!que.empty()){
        int cur = que.front(); que.pop();
        for(int i=1; i<=n; i++){
            if(dis[i]==-1 && cap[cur][i]>0){
                dis[i] = dis[cur]+1;
                que.push(i);
            }
        }
    }
    if(dis[t]==-1) return false;
    else return true;
}

int DFS(int cur, int low)
{
    if(cur == t) return low;
    int tmp=0;
    for(int i=1; i<=n; i++){
        if( cap[cur][i]>0 && dis[i] == dis[cur]+1 && (tmp = DFS(i, min(low, cap[cur][i])))){
            cap[cur][i] -= tmp;
            cap[i][cur] += tmp;
            return tmp;        //   增广路一条一条地找
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d%d", &m, &n)){
        memset(cap, 0, sizeof(cap));
        s = 1;
        t = n;
        for(int i=1; i<=m; i++){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            cap[u][v] += w;   //   记得是 += ,因为可能出现多条重复路径叠加。
        }
        int ans = 0, tmp=0;
        while(BFS()){
            if(tmp = DFS(s, INF))
                ans += tmp;
        }
        printf("%d\n", ans);

    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值