SGU 194 Reactor Cooling ——网络流
【题目分析】
无源汇上下界可行流。
上下界网络流的问题可以参考这里。↓
http://www.cnblogs.com/kane0526/archive/2013/04/05/3001108.html
【代码】
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
//#include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 205
#define me 50005
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
}
int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int h[me<<1],to[me<<1],ne[me<<1],fl[me<<1],en=0,S=0,T=me-1;
int id[me<<1];
void add(int a,int b,int c,int ID)
{
// cout<<"add "<<a<<" "<<b<<" "<<c<<endl;
to[en]=b; ne[en]=h[a]; fl[en]=c; id[en]=ID; h[a]=en++;
to[en]=a; ne[en]=h[b]; fl[en]=0; id[en]=0; h[b]=en++;
}
int map[me];
bool tell()
{
queue <int> q;
memset(map,-1,sizeof map);
map[S]=0;
while (!q.empty()) q.pop();
q.push(S);
while (!q.empty())
{
int x=q.front(); q.pop();
// cout<<"bfs"<<x<<endl;
for (int i=h[x];i>=0;i=ne[i])
{
// cout<<"to "<<to[i]<<endl;
if (map[to[i]]==-1&&fl[i]>0)
{
map[to[i]]=map[x]+1;
q.push(to[i]);
}
}
}
// cout<<"over"<<endl;
if (map[T]!=-1) return true;
return false;
}
int zeng(int k,int r)
{
if (k==T) return r;
int ret=0;
for (int i=h[k];i>=0&&ret<r;i=ne[i])
if (map[to[i]]==map[k]+1&&fl[i]>0)
{
int tmp=zeng(to[i],min(fl[i],r-ret));
ret+=tmp; fl[i]-=tmp; fl[i^1]+=tmp;
}
if (!ret) map[k]=-1;
return ret;
}
int ans[me<<1],n,m,du[me<<1],dn[me<<1];
int main()
{
Finout();
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(h,-1,sizeof h);
memset(ans,0,sizeof ans);
memset(du,0,sizeof du);
memset(dn,0,sizeof dn);
F(i,1,m)
{
int a,b,c,d;
a=Getint();b=Getint();c=Getint();d=Getint();
add(a,b,d-c,i);
du[a]-=c; du[b]+=c; dn[i]+=c;
}
F(i,1,n)
{
if (du[i]) add(S,i,du[i],0);
if (du[i]<0) add(i,T,-du[i],0);
}
int now=0,tmp=0;
while (tell()) while (tmp=zeng(S,inf)) now+=tmp;
int flag=1;
for (int i=h[S];i>=0;i=ne[i])
if (fl[i]>0) flag=0;
if (!flag) printf("NO\n");
else
{
printf("YES\n");
F(i,0,en-1) ans[id[i]]=fl[i^1];
F(i,1,m) printf("%d\n",ans[i]+dn[i]);
}
}
}
SGU 194 Reactor Cooling ——网络流的更多相关文章
- SGU 194 Reactor Cooling (无源上下界网络流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
- SGU 194 Reactor Cooling 无源汇带上下界可行流
Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...
- SGU 194 Reactor Cooling(无源无汇上下界可行流)
Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidin ...
- SGU 194. Reactor Cooling(无源汇有上下界的网络流)
时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...
- 【无源汇上下界最大流】SGU 194 Reactor Cooling
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=194 题目大意: n个点(n<20000!!!不是200!!!RE了无数次) ...
- SGU 194 Reactor Cooling
http://acm.sgu.ru/problem.php?contest=0&problem=194 题意:m条有向边,有上下界,求最大流. 思路:原图中有u-v low[i],high[i ...
- SGU 194 Reactor Cooling Dinic求解 无源无汇有上下界的可行流
题目链接 题意:有向图中有n(1 <= n <= 200)个点,无自环或者环的节点个数至少为3.给定每条边的最小流量和最大流量,问每条边的可行流量为多少? 思路:一般求解的网络流并不考虑下 ...
- sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...
- SGU 194 Reactor Cooling (有容量和下界的可行流)
题意:给定上一个有容量和下界的网络,让你求出一组可行解. 析:先建立一个超级源点 s 和汇点 t ,然后在输入时记录到每个结点的下界的和,建边的时候就建立c - b的最后再建立 s 和 t , 在建立 ...
随机推荐
- linux下环境变量PS1设置
PS1变量中提示符各项含义: \d :代表日期,格式为weekday month date,例如:Wed Dec 12 \H :完整的主机名称.例如:hostname是debian.linux \ ...
- 17232 伪Acmer的推理(传递闭包)
17232 伪Acmer的推理 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 收入:0 题型: 编程题 语言: G++;GCC Description 现在正是期末, ...
- java 对象的上转型对象(父类)
Example5_10.java class 类人猿 { void crySpeak(String s) { System.out.println(s); } } class People exten ...
- HDU 2444 The Accomodation of Students
首先是要构造二分图,然后二分图的最大匹配. 还有没完全证明过我的方法的正确性,但是AC了..... #include<cstdio> #include<cstring> #in ...
- Spring注解基本解读
在一个类中使用Spring对象,办法如下: 使用注解的形式注入 从ApplicationContext中获取. T t = new ApplicationContext.getBean("x ...
- HTML的TextArea标记跟随文本内容自动设置高度
js <textarea name="textarea" id="textarea" style='overflow-y: hidden;height:2 ...
- Linux SCP命令复制传输文件的用法
SCP命令是用户通过网络将一台Linux服务器的文件复制到另一台Linux服务器,方法如下: 一:从本地复制到远程 复制文件: 命令格式: scp local_file remote_username ...
- vim Podfile
platform :ios, "7.0"pod "AFNetworking"pod "SDWebImage"pod "SVProg ...
- CentOS 修改DNS,固定IP等操作(网络)
1.修改DNS 修改对应网卡的DNS的配置文件 vi /etc/resolv.conf 内容格式(西工大) nameserver 114.114.114.114 nameserver 202.117. ...
- Windows环境下使用VS2005编译OpenSSL
如何Windows环境下,使用VS2005编译OpenSSL,虽然这个问题在Baidu.Google上一堆,但安装中还是遇到些问题,在这里 记录下来希望能帮助大家不要在走弯路.注:我是在WinXP S ...