ZOJ2314 Reactor Cooling 无源汇有上下界最大流

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ2314 Reactor Cooling 无源汇有上下界最大流相关的知识,希望对你有一定的参考价值。

推荐看这里

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, m, uu, vv, ww, cc, hea[225], cnt, ss, tt, maxFlow, lev[225], tot, T;
const int oo=0x3f3f3f3f;
struct Edge{
    int too, nxt, cap, lim;
}edge[500005];
queue<int> d;
void add_edge(int fro, int too, int cap, int lim){
    edge[cnt].nxt = hea[fro];
    edge[cnt].too = too;
    edge[cnt].cap = cap;
    edge[cnt].lim = lim;
    hea[fro] = cnt++;
}
bool bfs(){
    memset(lev, 0, sizeof(lev));
    d.push(ss);
    lev[ss] = 1;
    while(!d.empty()){
        int x=d.front();
        d.pop();
        for(int i=hea[x]; i!=-1; i=edge[i].nxt){
            int t=edge[i].too;
            if(!lev[t] && edge[i].cap>0){
                lev[t] = lev[x] + 1;
                d.push(t);
            }
        }
    }
    return lev[tt]!=0;
}
int dfs(int x, int lim){
    if(x==tt)   return lim;
    int addFlow=0;
    for(int i=hea[x]; i!=-1 && addFlow<lim; i=edge[i].nxt){
        int t=edge[i].too;
        if(lev[t]==lev[x]+1 && edge[i].cap>0){
            int tmp=dfs(t, min(lim-addFlow, edge[i].cap));
            edge[i].cap -= tmp;
            edge[i^1].cap += tmp;
            addFlow += tmp;
        }
    }
    return addFlow;
}
void dinic(){
    while(bfs())    maxFlow += dfs(ss, oo);
}
int main(){
    cin>>T;
    while(T--){
        cin>>n>>m;
        cnt = tot = maxFlow = 0;
        memset(hea, -1, sizeof(hea));
        ss = 0; tt = n + 1;
        for(int i=1; i<=m; i++){
            scanf("%d %d %d %d", &uu, &vv, &cc, &ww);
            add_edge(uu, vv, ww-cc, cc);
            add_edge(vv, uu, 0, cc);
            add_edge(ss, vv, cc, cc);
            add_edge(vv, ss, 0, cc);
            add_edge(uu, tt, cc, cc);
            add_edge(tt, uu, 0, cc);
            tot += cc;
        }
        dinic();
        if(maxFlow<tot) printf("NO\n");
        else{
            printf("YES\n");
            for(int i=0; i<cnt; i+=6)
                printf("%d\n", edge[i^1].cap+edge[i].lim);
        }
        printf("\n");
    }
    return 0;
}

以上是关于ZOJ2314 Reactor Cooling 无源汇有上下界最大流的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ2314:Reactor Cooling——题解

ZOJ_2314_Reactor Cooling_有上下界可行流模板

Reactor Cooling ZOJ - 2314 上下界网络流

zoj 2314 Reactor Cooling (无源汇上下界可行流)

zoj 2314Reactor Cooling

ZOJ2314 Reactor Cooling