SGU 194. Reactor Cooling【无源汇上下界最大流】

前端之家收集整理的这篇文章主要介绍了SGU 194. Reactor Cooling【无源汇上下界最大流】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

题目链接http://acm.sgu.ru/problem.php?contest=0&problem=194

当所有附加边全部满流时(即maxflow==所有du[]>0之和),有可行解。

代码

#include <iostream> 
#include <algorithm> 
#include <set> 
#include <map> 
#include <string.h> 
#include <queue> 
#include <sstream> 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <string>

using namespace std;

const int MAXN = 1010;//点数的最大值
const int MAXM = 400100;//边数的最大值
const int INF = 0x3f3f3f3f;

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];//注意是MAXM

int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init()
{
    tol = 0;
    memset(head,-1,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw = 0)
{
    edge[tol].to = v; edge[tol].cap = w; edge[tol].next = head[u];
    edge[tol].flow = 0; head[u] = tol++;
    edge[tol].to = u; edge[tol].cap = rw; edge[tol].next = head[v];
    edge[tol].flow = 0; head[v] = tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u = start;
    pre[u] = -1;
    gap[0] = N;
    int ans = 0;
    while (dep[start] < N)
    {
        if (u == end)
        {
            int Min = INF;
            for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
                if (Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            for (int i = pre[u]; i != -1; i = pre[edge[i ^ 1].to])
            {
                edge[i].flow += Min;
                edge[i ^ 1].flow -= Min;
            }
            u = start;
            ans += Min;
            continue;
        }
        bool flag = false;
        int v;
        for (int i = cur[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].to;
            if (edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u])
            {
                flag = true;
                cur[u] = pre[v] = i;
                break;
            }
        }
        if (flag)
        {
            u = v;
            continue;
        }
        int Min = N;
        for (int i = head[u]; i != -1; i = edge[i].next)
            if (edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if (!gap[dep[u]])return ans;
        dep[u] = Min + 1;
        gap[dep[u]]++;
        if (u != start) u = edge[pre[u] ^ 1].to;
    }
    return ans;
}

int n,m;
int in[MAXN],least[MAXM],num[MAXM];

int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        init();
        memset(in,sizeof(in));
        memset(least,sizeof(least));

        int a,b,c,d;
        for (int i = 1;i <= m;i++)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            num[i] = tol;
            least[i] = c;
            in[a] -= c;
            in[b] += c;

            addedge(a,d - c);
        }
        int sum = 0;
        for (int i = 1;i <= n;i++)
        {
            if (in[i] > 0)
            {
                addedge(0,i,in[i]);
                sum += in[i];
            }
            else addedge(i,n + 1,-in[i]);
        }
        if (sap(0,n + 2) != sum) puts("NO");
        else
        {
            puts("YES");
            for (int i = 1;i <= m;i++) printf("%d\n",edge[num[i]].flow + least[i]);
        }

    }
    return 0;
}

猜你在找的React相关文章