【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)
Description
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:
f i,1+f i,2+...+f i,N = f 1,i+f 2,i+...+f N,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
描述
求有N个点M条边,每条边有上下界的网络图是否有可行解
题解
原边的流量改为上界-下界
然后计算每个点的入边的下界和-出边的下界和x
如果x>0从s向这个点连边,边权为x
如果x<0从t向这个点连边,边权为-x
判断与s相连的边是否都满流
代码
//by 减维
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<bitset>
#include<set>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<ctime>
#include<algorithm>
#define ll long long
#define li inline
#define rg register
#define db double
#define inf 1<<30
#define maxn 50010
#define eps 1e-8
using namespace std; inline int read()
{
int ret=;bool fla=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-'){fla=;ch=getchar();}
while(ch>=''&&ch<=''){ret=ret*+ch-'';ch=getchar();}
return fla?-ret:ret;
} struct edge{
int to,ne,cap;
}e[maxn<<]; int n,m,s,t,ans,sum,ecnt=,mn[maxn<<],du[maxn],layer[maxn],head[maxn]; void add(int x,int y,int v)
{
ecnt++;e[ecnt]=(edge){y,head[x],v};head[x]=ecnt;
ecnt++;e[ecnt]=(edge){x,head[y],};head[y]=ecnt;
} bool bfs()
{
memset(layer,,sizeof layer);layer[s]=;
queue<int>q;q.push(s);
while(!q.empty())
{
int d=q.front();q.pop();
for(int i=head[d];i;i=e[i].ne)
{
int dd=e[i].to;
if(e[i].cap>&&!layer[dd])
{
layer[dd]=layer[d]+;
if(dd==t)return ;
q.push(dd);
}
}
}
return ;
} int dfs(int x,int cap)
{
if(x==t||!cap) return cap;
int tmp,ret=;
for(int i=head[x];i;i=e[i].ne)
{
int dd=e[i].to;
if(e[i].cap&&layer[dd]==layer[x]+)
{
tmp=dfs(dd,min(cap,e[i].cap));
ret+=tmp;cap-=tmp;
e[i].cap-=tmp;e[i^].cap+=tmp;
}
}
return ret;
} void dinic()
{
while(bfs())ans+=dfs(s,inf);
} int main()
{
int T;
T=read();
while(T--){
ecnt=;sum=;ans=;
memset(head,,sizeof head);
memset(du,,sizeof du);
n=read(),m=read();
for(int i=,x,y,v;i<=m;++i)
{
x=read(),y=read(),mn[i]=read(),v=read();
add(x,y,v-mn[i]);
du[x]-=mn[i];
du[y]+=mn[i];
}
s=,t=n+;
for(int i=;i<=n;++i)
if(du[i]>) add(s,i,du[i]),sum+=du[i];
else add(i,t,-du[i]);
dinic();
if(sum!=ans){
puts("NO\n");
continue;
}else puts("YES");
for(int i=;i<=m;i++)
printf("%d\n",e[(i<<)+].cap+mn[i]);
puts("");
}
return ;
}
【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)的更多相关文章
- [ZOJ2341]Reactor Cooling解题报告|带上下界的网络流|无源汇的可行流
Reactor Cooling The terrorist group leaded by a well known international terrorist Ben Bladen is bul ...
- ZOJ 2314 Reactor Cooling 带上下界的网络流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的, ...
- SGU 194 Reactor Cooling (无源上下界网络流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
- 【zoj2314】Reactor Cooling 有上下界可行流
题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...
- ZOJ2314 Reactor Cooling(有上下界的网络流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
- SGU 194. Reactor Cooling(无源汇有上下界的网络流)
时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...
- 【ZOJ2314】Reactor Cooling(有上下界的网络流)
前言 话说有上下界的网络流好像全机房就我一个人会手动滑稽,当然这是不可能的 Solution 其实这道题目就是一道板子题,主要讲解一下怎么做无源无汇的上下界最大流: 算法步骤 1.将每条边转换成0~u ...
- ZOJ 2314 Reactor Cooling(无源汇上下界网络流)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题意: 给出每条边流量的上下界,问是否存在可行流,如果存在则输出. ...
- Reactor Cooling(无源汇有上下界网络流)
194. Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard o ...
随机推荐
- Android设计模式(九)--外观模式
问题:在Android中,Apk能够有微信,QQ为代表的插件式安装更新功能: 那么问题来了,主系统(姑且这么说)调用插件式安装的子系统.由子系统提供对外的訪问.属不属于一种外观模式呢? 先说设计模式: ...
- Java泛型深入理解
泛型的优点: 泛型的主要优点就是让编译器保留參数的类型信息,执行类型检查,执行类型转换(casting)操作,编译器保证了这些类型转换(casting)的绝对无误. /******* 不使用泛型类型 ...
- R语言与数据分析之八:时间序列--霍尔特指数平滑法
上篇我和小伙伴们分享了简单指数平滑法,简单指数平滑法仅仅能预測那些处于恒定水平和没有季节变动的时间序列,今天和大家分享非恒定水平即有增长或者减少趋势的.没有季节性可相加模型的时间序列预測算法---霍尔 ...
- Android之不须要自己定义View(ViewfindView.java)最简单的二维码扫描
不废话,先爆照 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- Lucene41PostingWriter源代码分析
原来看lucene4.0的posting格式(http://blog.csdn.net/jollyjumper/article/details/30017581),发现这还是比較简单的VInt格式,据 ...
- 关于python2.7从数据库读取中文显示乱码的问题解决
#!/usr/bin/env python # _*_ coding:utf-8 _*_ import MySQLdb import sys str = raw_input("please ...
- Effective Java 第三版——6. 避免创建不必要的对象
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 使用Mybatis进行多表联查操作
(1)增加一个测试数据库shop_order,sql语句如下: CREATE DATABASE `shop_order`; USE `shop_order`; CREATE TABLE `t_user ...
- 【java】多线程同步死锁
package 多线程; class A{ public synchronized void say(B b){ System.out.println("A说:你把你的本给我,我把我的笔给你 ...
- H5+混合移动app应用开发——开篇
前言 经过2个多月的艰苦奋斗,app的第一个版本已经快完工了,期间遇到了太多的坑,作为一个喜欢分享的人,我当然不会吝啬分享这爬坑历程.不要问我有多坑,我会告诉你很多,很多..... 过去一直从事.ne ...