码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-22 21:55:21      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:cto   body   tail   article   ring   pre   empty   ==   pos   

推荐看这里

#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 无源汇有上下界最大流

标签:cto   body   tail   article   ring   pre   empty   ==   pos   

原文地址:http://www.cnblogs.com/poorpool/p/8087538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!