LiberOJ ~ 115 ~ 无源汇有上下界可行流 (模板题)

 

//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;

const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;

struct Edge
{
    int from, to, cap, flow;       //起点,终点,容量,流量
    Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};
struct Dinic
{
    int n, m, s, t;                //结点数,边数(包括反向弧),源点s,汇点t
    vector<Edge> edges;            //边表。edges[e]和edges[e^1]互为反向弧
    vector<int> G[MAXN];           //邻接表,G[i][j]表示结点i的第j条边在edges数组中的序号
    int d[MAXN];                   //从起点到i的距离(层数差)
    int cur[MAXN];                 //当前弧下标
    bool vis[MAXN];                //BFS分层使用

    void init(int n)
    {
        this->n = n;
        edges.clear();
        for (int i = 0; i <= n; i++) G[i].clear();
    }

    void AddEdge(int from, int to, int cap)
    {
        edges.push_back(Edge(from, to, cap, 0));
        edges.push_back(Edge(to, from, 0, 0));
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }

    bool BFS()//构造分层网络
    {
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        d[s] = 0;
        vis[s] = true;
        Q.push(s);
        while (!Q.empty())
        {
            int x = Q.front(); Q.pop();
            for (int i = 0; i < G[x].size(); i++)
            {
                Edge& e = edges[G[x][i]];
                if (!vis[e.to] && e.cap > e.flow)
                {
                    vis[e.to] = true;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x, int a)//沿阻塞流增广
    {
        if (x == t || a == 0) return a;
        int flow = 0, f;
        for (int& i = cur[x]; i < G[x].size(); i++)//从上次考虑的弧
        {
            Edge& e = edges[G[x][i]];
            if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0)//多路增广
            {
                e.flow += f;
                edges[G[x][i]^1].flow -= f;
                flow += f;
                a -= f;
                if (a == 0) break;
            }
        }
        return flow;
    }
    int MaxFlow(int s, int t)
    {
        this->s = s; this->t = t;
        int flow = 0;
        while (BFS())
        {
            memset(cur, 0, sizeof(cur));
            flow += DFS(s, INF);
        }
        return flow;
    }

}solve;

int n, m, down[MAXN], up, sum[MAXN];

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        int tot = 0;
        memset(sum, 0, sizeof(sum));
        int vs = 0, vt = n+m+1;
        solve.init(vt);
        int cur = solve.edges.size();//cur记录需要输出边的起点
        for (int i = 0; i < m; i++)
        {
            int u, v; scanf("%d%d%d%d", &u, &v, &down[i], &up);
            solve.AddEdge(u, v, up-down[i]);
            sum[u] -= down[i];
            sum[v] += down[i];
        }
        for (int i = 0; i <= vt; i++)
        {
            if (sum[i] < 0) solve.AddEdge(i, vt, -sum[i]);
            else solve.AddEdge(vs, i, sum[i]), tot += sum[i];
        }
        int MF = solve.MaxFlow(vs, vt);//可行流
        if (MF != tot) printf("NO\n");
        else
        {
            printf("YES\n");
            for (int i = cur, j = 0; i < cur+m*2; i += 2, j++)
            {
                printf("%d\n", solve.edges[i].flow+down[j]);
            }
        }
    }
    return 0;
}

/*
4 6
1 2 1 3
2 3 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3
*/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值