sgu 194 上下界网络流可行流判定+输出可行流
#include <cstdio>
#include <cstring>
#define min(a,b) ((a)<(b)?(a):(b))
#define oo 0x3f3f3f3f
#define N 210
#define M 100010 struct Dinic {
int n, src, dst;
int head[N], dest[M], flow[M], eid[M], next[M], etot;
int cur[N], dep[N], qu[N], bg, ed; void init( int n, int src, int dst ) {
this->n = n;
this->src = src;
this->dst = dst;
etot = ;
memset( head, -, sizeof(head) );
}
void adde( int id, int u, int v, int f ) {
next[etot]=head[u], flow[etot]=f, dest[etot]=v, eid[etot]=id; head[u]=etot++;
next[etot]=head[v], flow[etot]=, dest[etot]=u, eid[etot]=-; head[v]=etot++;
}
bool bfs() {
memset( dep, , sizeof(dep) );
qu[bg=ed=] = src;
dep[src] = ;
while( bg<=ed ) {
int u=qu[bg++];
for( int t=head[u]; ~t; t=next[t] ) {
int v=dest[t], f=flow[t];
if( f && !dep[v] ) {
dep[v] = dep[u]+;
qu[++ed] = v;
}
}
}
return dep[dst];
}
int dfs( int u, int a ) {
if( u==dst || a== ) return a;
int remain=a, past=, na;
for( int &t=cur[u]; ~t; t=next[t] ) {
int v=dest[t], &f=flow[t], &vf=flow[t^];
if( f && dep[v]==dep[u]+ && (na=dfs(v,min(remain,f))) ) {
f -= na;
vf += na;
remain -= na;
past += na;
if( !remain ) break;
}
}
return past;
}
int maxflow() {
int f = ;
while( bfs() ) {
for( int u=; u<=n; u++ ) cur[u]=head[u];
f += dfs(src,oo);
}
return f;
}
void print( int *ans ) {
for( int e=; e<etot; e++ )
if( eid[e]!=- ) ans[eid[e]]+=flow[e^];
}
}D;
struct Bottop {
int n;
int head[N], dest[M], bot[M], top[M], next[M], eid[M], etot;
int si[N], so[N], sum; void init( int n ) {
this->n = n;
memset( head, -, sizeof(head) );
memset( si, , sizeof(si) );
memset( so, , sizeof(so) );
etot = ;
}
void adde( int id, int u, int v, int b, int t ) {
eid[etot]=id, next[etot]=head[u], bot[etot]=b, top[etot]=t, dest[etot]=v;
head[u]=etot++;
}
void build( int *ans ) {
int src=n+, dst=n+;
D.init( dst, src, dst );
for( int u=; u<=n; u++ )
for( int t=head[u]; ~t; t=next[t] ) {
int v=dest[t];
ans[eid[t]] += bot[t];
si[v] += bot[t];
so[u] += bot[t];
D.adde( eid[t], u, v, top[t]-bot[t] );
}
for( int u=; u<=n; u++ ) {
if( so[u]>si[u] )
D.adde( -, u, dst, so[u]-si[u] );
else if( si[u]>so[u] ) {
D.adde( -, src, u, si[u]-so[u] );
sum += si[u]-so[u];
}
}
}
bool ok() { return D.maxflow()==sum; }
}B; int n, m;
int ans[M]; int main() {
scanf( "%d%d", &n, &m );
B.init(n);
for( int i=,u,v,b,t; i<=m; i++ ) {
scanf( "%d%d%d%d", &u, &v, &b, &t );
B.adde( i, u, v, b, t );
}
B.build(ans);
bool ok = B.ok();
printf( "%s\n", ok ? "YES" : "NO" );
if( ok ) {
D.print( ans );
for( int i=; i<=m; i++ )
printf( "%d\n", ans[i] );
}
}
sgu 194 上下界网络流可行流判定+输出可行流的更多相关文章
- sgu 176 上下界网络流最小可行流带输出方案
算法步骤: 1. 先将原图像最大可行流那样变换,唯一不同的是不加dst->src那条边来将它变成无源无汇的网络流图.直接跑一边超级源到超级汇的最大流. 2. 加上刚才没有加上的那条边p 3. 再 ...
- hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )
题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...
- [BZOJ2502]清理雪道 有上下界网络流(最小流)
2502: 清理雪道 Time Limit: 10 Sec Memory Limit: 128 MB Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...
- 算法笔记--最大流和最小割 && 最小费用最大流 && 上下界网络流
最大流: 给定指定的一个有向图,其中有两个特殊的点源S(Sources)和汇T(Sinks),每条边有指定的容量(Capacity),求满足条件的从S到T的最大流(MaxFlow). 最小割: 割是网 ...
- 【上下界网络流 二分】bzoj2406: 矩阵
感觉考试碰到上下界网络流也还是写不来啊 Description Input 第一行两个数n.m,表示矩阵的大小. 接下来n行,每行m列,描述矩阵A. 最后一行两个数L,R. Output 第一行,输出 ...
- SGU 194 Reactor Cooling ——网络流
[题目分析] 无源汇上下界可行流. 上下界网络流的问题可以参考这里.↓ http://www.cnblogs.com/kane0526/archive/2013/04/05/3001108.html ...
- POJ 2396 Budget(有源汇上下界网络流)
Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...
- HDU 4940 Destroy Transportation system(无源汇上下界网络流)
Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...
- uoj132/BZOJ4200/洛谷P2304 [Noi2015]小园丁与老司机 【dp + 带上下界网络流】
题目链接 uoj132 题解 真是一道大码题,,,肝了一个上午 老司机的部分是一个\(dp\),观察点是按\(y\)分层的,而且按每层点的上限来看可以使用\(O(nd)\)的\(dp\),其中\(d\ ...
随机推荐
- Explorer : 发布一个key-value存储系统,带有客户端和服务器端
Explorer 一个key-value存储系统,带有客户端和服务器端.使用非常方便. 使用B+树作为存储引擎,客户端和服务器端使用TCP协议进行通信. 代码采用C++实现,底层将客户端和服务器通信封 ...
- MySQL增量备份与恢复实例【转】
小量的数据库可以每天进行完整备份,因为这也用不了多少时间,但当数据库很大时,就不太可能每天进行一次完整备份了,这时候就可以使用增量备份.增量备份的原理就是使用了mysql的binlog日志.本次操作的 ...
- MySQL服务器修改主机名后问题解决
1.单机MySQL主机名修改 今天无事看到自己的主机名不对,于是改了一下,以便区分服务器,那只重启MySQL时出现下面错误: MySQL manager or server PID file coul ...
- 2018 CCPC网络赛
2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物 ...
- HDU 6199 2017沈阳网络赛 DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6199 题意:n堆石子,Alice和Bob来做游戏,一个人选择取K堆那么另外一个人就必须取k堆或者k+1 ...
- 32 Profiling Go Programs 分析go语言项目
Profiling Go Programs 分析go语言项目 24 June 2011 At Scala Days 2011, Robert Hundt presented a paper titl ...
- SpringMVC 返回JSON数据的配置
spring-mvc-config.xml(文件名称请视具体情况而定)配置文件: <!-- 启动Springmvc注解驱动 --> <mvc:annotation-driven> ...
- Go 命令行总结
go build:已当前目录作为package进行编译,将当前目录下的所有文件编译成package文件,文件名与所在的目录同名. go install: 分两步操作:1.先执行go build进行编译 ...
- 使用vs2015编辑c++模板程序报错2019
笔者这几天在熟悉vs2015工具编辑c++,(从前用的都是vc++6.0).发现还真是不容易使用,简单的一个小程序会报错一大堆你看不懂的问题.现将我发现的重要的一个问题呈现给大家. 在使用类模板设计对 ...
- Codeforces 734C Anton and Making Potions(枚举+二分)
题目链接:http://codeforces.com/problemset/problem/734/C 题目大意:要制作n个药,初始制作一个药的时间为x,魔力值为s,有两类咒语,第一类周瑜有m种,每种 ...