最大流之上下界可行流

题目:AcWing 2188. 无源汇上下界可行流

分析:每条边的容量范围整体减小下限个单位,不足用源点汇点补。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
struct edge
{
    int ep, val, nex;
}e[maxn];
int head[maxn], tot;
int n, m, s, t;
int depth[maxn], cur[maxn];
int q[maxn];
int low[maxn];
int dif[maxn];

void init()
{
    mem(head, -1), tot = 0;
}

void addedge(int x, int y, int a, int b)
{
    e[tot] = {y, b - a, head[x]}, low[tot] = a;
    head[x] = tot++;
    e[tot] = {x, 0, head[y]};
    head[y] = tot++;
}

bool bfs()
{
    int hh = 0, tt = 0;
    q[tt] = s;
    mem(depth, -1), depth[s] = 1;
    cur[s] = head[s];
    while(hh <= tt)
    {
        int now = q[hh++];
        for(int i = head[now]; ~i; i = e[i].nex)
        {
            int ep = e[i].ep;
            if(depth[ep] == -1 && e[i].val)
            {
                cur[ep] = head[ep];
                depth[ep] = depth[now] + 1;
                if(ep == t) return true;
                q[++tt] = ep;
            }
        }
    }
    return false;
}

int dfs(int now, int flow)
{
    if(now == t) return flow;
    int res = 0;
    for(int i = head[now]; ~i && flow; i = e[i].nex)
    {
        cur[now] = i;
        int ep = e[i].ep;
        if(depth[ep] == depth[now] + 1 && e[i].val)
        {
            int k = dfs(ep, min(flow, e[i].val));
            if(k == 0) depth[ep] = -1;
            e[i].val -= k, e[i ^ 1].val += k;
            flow -= k, res += k;
        }
    }
    return res;
}

int dinic()
{
    int res = 0;
    while(bfs())
        res += dfs(s, inf);
    return res;
}

int main()
{
    cin >> n >> m;
    init();
    s = 0, t = n + 1;
    for(int i = 1; i <= m; i++)
    {
        int x, y, a, b; cin >> x >> y >> a >> b;
        addedge(x, y, a, b);
        dif[x] -= a, dif[y] += a;
    }
    int sum = 0;
    for(int i = 1; i <= n; i++)
        if(dif[i] > 0) addedge(s, i, 0, dif[i]), sum += dif[i];
        else if(dif[i] < 0) addedge(i, t, 0, -dif[i]);
    int d = dinic();
    if(d != sum)  cout << "NO" << endl;
    else
    {
        cout << "YES" << endl;
        for(int i = 0; i < m << 1; i += 2)
        {
            cout << low[i] + e[i ^ 1].val << endl;
        }
    }
}

题目:2189. 有源汇上下界最大流

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
struct edge
{
    int ep, val, nex;
}e[maxn];
int head[maxn], tot;
void init()
{
    mem(head, -1), tot = 0;
}

void addedge(int x, int y, int val)
{
    e[tot] = {y, val, head[x]};
    head[x] = tot++;
    e[tot] = {x, 0, head[y]};
    head[y] = tot++;
}
int add[maxn];
int S, T;
int n, m, s, t;
int depth[maxn], cur[maxn];
int q[maxn];


bool bfs()
{
    int hh = 0, tt = 0; q[hh] = S;
    mem(depth, -1), depth[S] = 1;   
    cur[S] = head[S];
    while(hh <= tt)
    {
        int now = q[hh++];
        for(int i = head[now]; ~i; i = e[i].nex)
        {
            int ep = e[i].ep;
            if(depth[ep] == -1 && e[i].val)
            {
                depth[ep] = depth[now] + 1;
                cur[ep] = head[ep];
                if(ep == T) return true;
                q[++tt] = ep;
            }
        }
    }
    return false;
}

int dfs(int now, int flow)
{
    if(now == T) return flow;
    int sum = 0;
    for(int i = cur[now]; ~i && flow; i = e[i].nex)
    {
        cur[now] = i;
        int ep = e[i].ep;
        if(depth[ep] == depth[now] + 1 && e[i].val)
        {
            int k = dfs(ep, min(flow, e[i].val));
            if(k == 0) depth[ep] = -1;
            e[i].val -= k, e[i ^ 1].val += k;
            sum += k, flow -= k;
        }
    }
    return sum;
}

int dinic()
{
    int res = 0;
    while(bfs())
        res += dfs(S, inf);
    return res;
}

int main()
{
    init();
    cin >> n >> m >> s >> t;
    for(int i = 1; i <= m; i++)
    {
        int x, y, a, b; cin >> x >> y >> a >> b;
        addedge(x, y, b - a);
        add[x] -= a, add[y] += a;
    }
    S = 0, T = n + 1;
    int sum = 0;
    for(int i = 1; i <= n; i++)
    {
        if(add[i] > 0) addedge(S, i, add[i]), sum += add[i];
        else if(add[i] < 0) addedge(i, T, -add[i]);
    }
    addedge(t, s, inf);
    if(sum != dinic()) cout << "No Solution" << endl;
    else
    {
        int res = e[tot - 1].val;
        e[tot - 1].val = e[tot - 2].val = 0;
        S = s, T = t;
        cout << res + dinic() << endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值