Code:

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=600;
const int INF=1000000;
# define pb push_back
int A[maxn],mapp[INF],bass[INF];
int s,t;
struct Edge{
int from,to,cap;
Edge(int u,int v,int c):from(u),to(v),cap(c) {}
};
vector<Edge>edges;
struct Dicnic{
vector<int>G[maxn];
int d[maxn],vis[maxn],cur[maxn];
queue<int>Q;
void init(){
for(int i=0;i<maxn;++i)G[i].clear();
edges.clear();
}
void addedge(int u,int v,int c,int cnt){
edges.pb(Edge(u,v,c)); //正向弧
edges.pb(Edge(v,u,0)); //反向弧
int m=edges.size();
G[u].pb(m-2);
G[v].pb(m-1);
if(cnt>0)mapp[cnt]=m-1;
}
int BFS()
{
memset(vis,0,sizeof(vis));
d[s]=0,vis[s]=1;Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
int sz=G[u].size();
for(int i=0;i<sz;++i){
Edge e=edges[G[u][i]];
if(!vis[e.to]&&e.cap>0){
d[e.to]=d[u]+1,vis[e.to]=1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a){
if(x==t)return a;
int sz=G[x].size();
int f,flow=0;
for(int i=cur[x];i<sz;++i){
Edge e=edges[G[x][i]];
cur[x]=i;
if(d[e.to]==d[x]+1&&e.cap>0){
f=dfs(e.to,min(a,e.cap));
if(f)
{
int u=G[x][i];
a-=f;
edges[u].cap-=f;
edges[u^1].cap+=f;
flow+=f;
if(a==0)break;
}
}
}
return flow;
}
int maxflow(){
int ans=0;
while(BFS()){
memset(cur,0,sizeof(cur));
ans+=dfs(s,INF);
}
return ans;
}
}op;
int main(){
int T;cin>>T;
s=0,t=420;
for(int cas=1;cas<=T;++cas){
memset(A,0,sizeof(A));
op.init();
int n,m,sum=0;
cin>>n>>m;
for(int i=1;i<=m;++i){
int a,b,c,d;
cin>>a>>b>>c>>d;
bass[i]=c;
op.addedge(a,b,d-c,i);
A[a]-=c,A[b]+=c;
}
for(int i=1;i<=m;++i){
if(A[i]>0){op.addedge(s,i,A[i],0);sum+=A[i];}
if(A[i]<0)op.addedge(i,t,-A[i],0);
}
int flow=op.maxflow();
if(flow==sum){
cout<<"YES"<<endl;
for(int i=1;i<m;++i)cout<<edges[mapp[i]].cap+bass[i]<<endl;
cout<<edges[mapp[m]].cap+bass[m];
if(cas!=T)cout<<endl;
}else cout<<"NO"<<endl;
if(cas!=T)cout<<endl;
}
return 0;
}

  

Reactor Cooling ZOJ - 2314 上下界网络流的更多相关文章

  1. ZOJ 2314 Reactor Cooling [无源汇上下界网络流]

    贴个板子 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...

  2. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

  3. ZOJ2314 Reactor Cooling(有上下界的网络流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  4. 【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)

     Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidi ...

  5. 【ZOJ2314】Reactor Cooling(有上下界的网络流)

    前言 话说有上下界的网络流好像全机房就我一个人会手动滑稽,当然这是不可能的 Solution 其实这道题目就是一道板子题,主要讲解一下怎么做无源无汇的上下界最大流: 算法步骤 1.将每条边转换成0~u ...

  6. ZOJ2314 Reactor Cooling(无源汇上下界可行流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  7. 【SGU194&ZOJ2314】Reactor Cooling(有上下界的网络流)

    题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质. 并且满足每根pipe一定的流 ...

  8. 【有上下界网络流】【ZOJ】2314 Reactor Cooling

    [算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...

  9. ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】

    题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...

随机推荐

  1. 常用sql调优《一》

    1. 使用索引,避免在索引列上使用计算. 2.用>=替代> <=替代< 3.用UNION替换OR (适用于索引列) 4.用IN来替换OR 或者 用EXISTS替代IN 5 用W ...

  2. css——应用多个样式

    应用多个样式 在class中使用多个样式 在这是会有优先级关系问题 在上面的代码中,aa,bb,中的颜色会有冲突,到底显示的结果会是黄色还是绿色呢? 结果是绿色的.它是以程序执行的先后为优先级,后执行 ...

  3. oracle中单引号的处理

    当想让输出的结果中字段带有单引号', 场景一:连续三个单引号''' select '''helin''' from dual; ---'helin' 场景二:拼接字段的结果集--连续4个单引号 sel ...

  4. GitHub两种上传方式的对比----SSH / https

    https://www.jianshu.com/p/1ac06bcd8ab5 https://www.cnblogs.com/lqfxyy/p/5740720.html https://blog.cs ...

  5. [USACO17JAN] Subsequence Reversal序列反转 (dfs+记忆化)

    题目大意:给你一个序列,你可以翻转任意一段子序列一次,求最长不下降子序列长度 tips:子序列可以不连续,但不能破坏在原序列中的顺序 观察数据范围,n<=50,很小,考虑dfs *dfs来跑区间 ...

  6. 中国剩余定理(excrt) 模板

    excrt板子题 #include <cmath> #include <cstdio> #include <cstring> #include <algori ...

  7. tyvj1864 [Poetize I]守卫者的挑战

    期望dp 不考虑打的顺序,只要最后能把地图都装下就行了,因此把概率和容量装进一个结构体里面,把地图放后面. 记一个状态f[i][j][k]表示前i个比赛,赢了j场,容量k的期望,转移即可. #incl ...

  8. 紫书 习题7-8 UVa 12107 (IDA*)

    参考了这哥们的博客 https://blog.csdn.net/hyqsblog/article/details/46980287  (1)atoi可以char数组转int, 头文件 cstdlib ...

  9. 中山纪念中学培训杂题(难的都不在这里面qwq)

    来中山纪中半个月了,差不多就要结束了, 写一些之前考试能更正的题解吧,还有一些不是给人做的(比如IOI2018互测.. 备注:我不会的就没有放上来了,所有数学有关的基本上都死了. 所以这里的题目都是相 ...

  10. How-to-quick-getting-started-for-Frontend

    一转眼已一年多不专注前端方面的开发工作,这一年前端方面的技术又是新天地,偶然接到内部团队邀请我给他们做一个前端的讲座,希望能帮助他们快速.且深刻了解前端这个行业以及行业内的知识,这可真有点让我为难,由 ...