ZOJ2314 Reactor Cooling(有上下界的网络流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for the nuclear bomb they are planning to create. Being the wicked computer genius of this group, you are responsible for developing the cooling system for the reactor.
The cooling system of the reactor consists of the number of pipes that special cooling liquid flows by. Pipes are connected at special points, called nodes, each pipe has the starting node and the end point. The liquid must flow by the pipe from its start point to its end point and not in the opposite direction.
Let the nodes be numbered from 1 to N. The cooling system must be designed so that the liquid is circulating by the pipes and the amount of the liquid coming to each node (in the unit of time) is equal to the amount of liquid leaving the node. That is, if we designate the amount of liquid going by the pipe from i-th node to j-th as fij, (put fij = 0 if there is no pipe from node i to node j), for each i the following condition must hold:
fi,1+fi,2+...+fi,N = f1,i+f2,i+...+fN,i
Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be fij <= cij where cij is the capacity of the pipe. To provide sufficient cooling, the amount of the liquid flowing by the pipe going from i-th to j-th nodes must be at least lij, thus it must be fij >= lij.
Given cij and lij for all pipes, find the amount fij, satisfying the conditions specified above.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
The first line of the input file contains the number N (1 <= N <= 200) - the number of nodes and and M - the number of pipes. The following M lines contain four integer number each - i, j, lij and cij each. There is at most one pipe connecting any two nodes and 0 <= lij <= cij <= 10^5 for all pipes. No pipe connects a node to itself. If there is a pipe from i-th node to j-th, there is no pipe from j-th node to i-th.
Output
On the first line of the output file print YES if there is the way to carry out reactor cooling and NO if there is none. In the first case M integers must follow, k-th number being the amount of liquid flowing by the k-th pipe. Pipes are numbered as they are given in the input file.
Sample Input
2
4 6
1 2 1 2
2 3 1 2
3 4 1 2
4 1 1 2
1 3 1 2
4 2 1 2
4 6
1 2 1 3
2 3 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3
Sample Input
NO
YES
1
2
3
2
1
1
有上下界的网络流可行流:
- #include<cstdio>
- #include<cstdlib>
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- using namespace std;
- const int maxn=;
- const int inf=;
- int Laxt[maxn],Next[maxn],To[maxn],Cap[maxn],cnt;
- int dis[maxn],nd[maxn],S,T,num,ans,q[maxn],qnum[maxn],top;
- void init()
- {
- cnt=;ans=num=top=;
- memset(Laxt,,sizeof(Laxt));
- memset(dis,,sizeof(dis));
- memset(nd,,sizeof(nd));
- }
- int add(int u,int v,int c)
- {
- Next[++cnt]=Laxt[u];
- Laxt[u]=cnt;
- To[cnt]=v;
- Cap[cnt]=c;
- Next[++cnt]=Laxt[v];
- Laxt[v]=cnt;
- To[cnt]=u;
- Cap[cnt]=;
- }
- int sap(int u,int flow)
- {
- if(u==T||flow==) return flow;
- int delta=,tmp;
- for(int i=Laxt[u];i;i=Next[i]){
- int v=To[i];
- if(dis[v]+==dis[u]&&Cap[i]>){
- tmp=sap(v,min(Cap[i],flow-delta));
- delta+=tmp;
- Cap[i]-=tmp;
- Cap[i^]+=tmp;
- if(flow==delta||dis[]>=T) return delta;
- }
- }
- nd[dis[u]]--;
- if(nd[dis[u]]==) dis[]=T;
- nd[++dis[u]]++;
- return delta;
- }
- int main()
- {
- int Case,n,i,j,m,u,v,x,y;
- scanf("%d",&Case);
- while(Case--){
- init();
- scanf("%d%d",&n,&m);
- S=;T=n+;
- for(i=;i<=m;i++){
- scanf("%d%d%d%d",&u,&v,&x,&y);
- u++;v++;num+=x;
- add(u,v,y-x);
- q[++top]=cnt;
- qnum[top]=x;
- add(S,v,x);
- add(u,T,x);
- }
- while(dis[S]<T) {
- ans+=sap(S,inf);
- }
- if(num!=ans) printf("NO\n");
- else {
- printf("YES\n");
- for(i=;i<=top;i++) printf("%d\n",Cap[q[i]]+qnum[i]);
- }
- if(T) printf("\n");
- }
- return ;
- }
分享三张图
看完后应该就能理解了。具体的以后再慢慢整理咯。
ZOJ2314 Reactor Cooling(有上下界的网络流)的更多相关文章
- ZOJ 2314 Reactor Cooling 带上下界的网络流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的, ...
- 【zoj2314】Reactor Cooling 有上下界可行流
题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...
- SGU 194 Reactor Cooling (无源上下界网络流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
- 【ZOJ2314】Reactor Cooling(有上下界的网络流)
前言 话说有上下界的网络流好像全机房就我一个人会手动滑稽,当然这是不可能的 Solution 其实这道题目就是一道板子题,主要讲解一下怎么做无源无汇的上下界最大流: 算法步骤 1.将每条边转换成0~u ...
- SGU 194. Reactor Cooling(无源汇有上下界的网络流)
时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...
- ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)
//有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...
- ZOJ 2314 有上下界的网络流
problemCode=2314">点击打开链接 题意:给定m条边和n个节点.每条边最少的流量和最多的流量.保证每一个节点的出入流量和相等,问能够形成吗,能够则输出每条边的流量 思路: ...
- poj_2396 有上下界的网络流
题目大意 一个mxn的矩阵,给出矩阵中每一行的和sh[1,2...m]以及每一列的数字的和目sv[1,2...n],以及矩阵中的一些元素的范围限制,比如a[1][2] > 1, a[2][3] ...
- 【BZOJ2502】清理雪道 有上下界的网络流 最小流
[BZOJ2502]清理雪道 Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降 ...
随机推荐
- 【转】python面向对象中的元类
type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello.py模块: class Hel ...
- RHEL 5 安装gcc
rpm -ivh kernel-headers... rpm -ivh glibc-headers... rpm -ivh glibc-devel... rpm -ivh libgomp.. rpm ...
- 对象数组空指针异常说明——C#中使用对象数组必须分别为其开辟空间
l 场景 定义一个学生类,包含字段(学号,姓名,语文成绩,数学成绩,英语成绩).属性(总成绩).三个方法分别为(求平均分.数学平均分.语文平均分). 要求:在main()方法中,定义一个学生类型的数 ...
- shell复制除了某个文件的操作
将app的已经写成带有日期名的日志放到归档特定目录(刨除正在记录的日志) find $APPHOME/logs | grep -v "info.log\|debug.log\|error.l ...
- Ubuntu 配置NTP Server
Ubuntu安装NTP Server很简单,分位3步走: 第一步:安装NTP root@cephadmin:~/ceph-cluster# apt-get install ntp Reading pa ...
- RDLC 微软报表 导出Excel时产生多个工作表 (worksheet)
. I have added two obejcts data source to Report Viewer. 2. in RDLC i have created two tables and in ...
- web-view和wx.navigateback
web-view 我们先来了解一下官方的东西 web-view 组件是一个可以用来承载网页的容器,会自动铺满整个小程序页面.个人类型与海外类型的小程序暂不支持使用. 属性名 类型 默认值 说明 src ...
- 2.mysql高级查询
01.SQL高级查询_排序 1.排序语句:order by 排序字段名 asc(默认的-升序) / desc(降序); 2.例如:查询所有服装类商品,将查询结果以价格升序排序: ...
- linux学习(rz和sz命令的安装和使用)
lrzsz的安装 [root@spark1 ~]# yum install lrzsz rz用法 终端直接输入rz,出现文件选择对话框,选择要上传的文件就ok sz用法 下载filename文件: s ...
- SQL查询顺序
SQL查询顺序 1 FROM 2 WHERE 3 SELECT 4 ORDER BY 5 GOUP BY 6 HAVING 左外连接:查询出 left join 左边表的全部数据,left join右 ...