sgu194:http://acm.sgu.ru/problem.php?contest=0&problem=194

题意:题目大意:给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质。并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li。

题解:题解:。对于每根管子有一个上界容量up和一个下界容量low,我们让这根管子的容量下界变为0,上界为up-low。可是这样做了的话流量就不守恒了,为了再次满足流量守恒,即每个节点"入流=出流”,我们增设一个超级源点st和一个超级终点sd。我们开设一个数组du[]来记录每个节点的流量情况。du[i]=in[i](i节点所有入流下界之和)-out[i](i节点所有出流下界之和)。当du[i]大于0的时候,st到i连一条流量为du[i]的边。当du[i]小于0的时候,i到sd连一条流量为-du[i]的边。最后对(st,sd)求一次最大流即可,如果从源点流出的边都满流了,则就有可行解,否则无解。

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define min(a,b)(a<b?a:b)
#define INF 1000000
using namespace std;
const int MAX=;
struct Node {
int c;
int f;
};
int sx,ex;//sx和ex分别代表源点和汇点
int pre[MAX];
Node map[][];
int n,m;
int du[MAX];
int dx[MAX],ll[MAX],rr[MAX];//记录每条边的流量下界,起点,终点
bool BFS() { //BFS搜索层次网络
memset(pre,,sizeof(pre));
queue< int > Q;
Q.push(sx);
pre[sx]=;
while(!Q.empty()) {
int d=Q.front();
Q.pop();
for(int i=; i<=n+; i++) {
if(!pre[i]&&map[d][i].c-map[d][i].f) {
pre[i]=pre[d]+;
Q.push(i);
}
}
}
return pre[ex]!=;
}
int dinic(int pos,int flow) { //pos是顶点号,flow是当前顶点所能得到的流量,一次dinic只能求出一次增加的流量,
int f=flow;
if(pos==ex)
return flow;
for(int i=; i<=+n; i++) {
if(map[pos][i].c-map[pos][i].f&&pre[pos]+==pre[i]) {
int a=map[pos][i].c-map[pos][i].f;
int t=dinic(i,min(a,flow));
map[pos][i].f+=t;
map[i][pos].f-=t;
flow-=t;
if(flow<=)break;
//我最开始就是这里没弄明白,我不明白为什么要此顶点得到的流量减去改变量;
//答案就在下面的 return f-flow;
}
}
if(f-flow<=)pre[pos]=-;
return f-flow;//其实这里返回给他前一层的就是这个t;因为t在层函数里面都有,所以所过避免重复就写成这样;
}
void slove(){
int sum=;
while(BFS()) {
sum+=dinic(sx,INF);
}
//return sum;
}
int main() {
int u,v,t1,t2;
while(cin>>n>>m) {
memset(du,,sizeof(du));
sx=;
ex=n+;
memset(map,,sizeof(map)) ;
for(int i=;i<=m; i++) {
cin>>u>>v>>t1>>t2;
map[u][v].c+=t2-t1;
du[u]-=t1;
du[v]+=t1;
dx[i]=t1;
ll[i]=u;
rr[i]=v;
}
for(int i=;i<=n;i++){
if(du[i]>)map[][i].c+=du[i];
if(du[i]<)map[i][n+].c+=(-du[i]);
}
slove();
bool flag=false;
for(int i=;i<=n;i++){
if(du[i]>){
if(map[][i].f!=map[][i].c){
flag=true;
break;
} }
}
if(flag)printf("NO\n");
else{
printf("YES\n");
for(int i=;i<=m;i++){
printf("%d\n",map[ll[i]][rr[i]].f+dx[i]);
}
}
}
return ;
}

Reactor Cooling的更多相关文章

  1. ZOJ2314 Reactor Cooling

    Reactor Cooling Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge The terrorist g ...

  2. zoj Reactor Cooling

    Reactor Cooling 无源汇上下界最大流问题. 1.流量平衡. 2.满足上下界 模板题. #include <iostream> #include <queue> # ...

  3. acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】

    称号:acdream 1211 Reactor Cooling 分类:无汇的有上下界网络流. 题意: 给n个点.及m根pipe,每根pipe用来流躺液体的.单向的.每时每刻每根pipe流进来的物质要等 ...

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

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

  5. ZOJ 1314 Reactor Cooling | 上下界无源汇可行流

    ZOJ 1314 Reactor Cooling | 上下界无源汇可行流 题意 有一个网络,每条边有流量的上界和下界,求一种方案,让里面的流可以循环往复地流动起来. 题解 上下界无源汇可行流的模型: ...

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

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

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

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

  8. SGU 194 Reactor Cooling(无源无汇上下界可行流)

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

  9. 【zoj2314】Reactor Cooling 有上下界可行流

    题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...

  10. [ZOJ2341]Reactor Cooling解题报告|带上下界的网络流|无源汇的可行流

    Reactor Cooling The terrorist group leaded by a well known international terrorist Ben Bladen is bul ...

随机推荐

  1. EF 命令

    我们选VS中工具-库程序包管理器-程序包管理器控制台, 这时在VS底部会出现控制台 这时候我们要执行四个操作: 下载安装EF 到工程.PM> Install-Package EntityFram ...

  2. hdu2047.java递推题

    阿牛的EOF牛肉串 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  3. 理解FTP协议

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/5186117. ...

  4. 为当前的div 动态添加一个样式

    $("#target").addClass("newClass");

  5. C#之垃圾回收

    垃圾回收时现代语言的标志之一.垃圾回收解放了手工管理对象释放的工作,提高了程序的健壮性,但是副作用就是程序代码可以对于创建对象变得随意. 1.避免不必要的对象创建 由于垃圾回收的代价较高,所以C#程序 ...

  6. json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...

  7. Tomcat - java.lang.UnsupportedClassVersionError:Unsupported major.minor version 51.0 (unable to load class com.microsoft.sqlserver.jdbc.SQLS

    今天使用Tomcat连接sql Server 2008 enterprise的时候,报错: HTTP Status 500 - Servlet execution threw an exception ...

  8. Windows phone 之手势识别(Drag)

    首先,在XAML页面中,添加如下代码: 前提,别忘了添加 1.引入dll (silverlight for wndows phone toolkit) 2.引入命名空间 xmlns:toolkit=& ...

  9. 如何禁用easyui-linkbutton 中的Click事件

    eg: <a id="btn" href="#" class="easyui-linkbutton" data-options=&qu ...

  10. jQuery插件综合应用(二)文字为主的页面

    一.介绍 文字内容是每个网站都有的内容,网站在展示文字内容时,总是比图片.视频等富媒体内容要难一些,因为富媒体容易被用户接受.尤其是越多的文字内容越难以被用户通篇的阅读,跳跃式阅读往往是阅读的主要方式 ...