#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 上下界网络流可行流判定+输出可行流的更多相关文章

  1. sgu 176 上下界网络流最小可行流带输出方案

    算法步骤: 1. 先将原图像最大可行流那样变换,唯一不同的是不加dst->src那条边来将它变成无源无汇的网络流图.直接跑一边超级源到超级汇的最大流. 2. 加上刚才没有加上的那条边p 3. 再 ...

  2. hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )

    题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...

  3. [BZOJ2502]清理雪道 有上下界网络流(最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

  4. 算法笔记--最大流和最小割 && 最小费用最大流 && 上下界网络流

    最大流: 给定指定的一个有向图,其中有两个特殊的点源S(Sources)和汇T(Sinks),每条边有指定的容量(Capacity),求满足条件的从S到T的最大流(MaxFlow). 最小割: 割是网 ...

  5. 【上下界网络流 二分】bzoj2406: 矩阵

    感觉考试碰到上下界网络流也还是写不来啊 Description Input 第一行两个数n.m,表示矩阵的大小. 接下来n行,每行m列,描述矩阵A. 最后一行两个数L,R. Output 第一行,输出 ...

  6. SGU 194 Reactor Cooling ——网络流

    [题目分析] 无源汇上下界可行流. 上下界网络流的问题可以参考这里.↓ http://www.cnblogs.com/kane0526/archive/2013/04/05/3001108.html ...

  7. POJ 2396 Budget(有源汇上下界网络流)

    Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...

  8. HDU 4940 Destroy Transportation system(无源汇上下界网络流)

    Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...

  9. uoj132/BZOJ4200/洛谷P2304 [Noi2015]小园丁与老司机 【dp + 带上下界网络流】

    题目链接 uoj132 题解 真是一道大码题,,,肝了一个上午 老司机的部分是一个\(dp\),观察点是按\(y\)分层的,而且按每层点的上限来看可以使用\(O(nd)\)的\(dp\),其中\(d\ ...

随机推荐

  1. css给表格每一列设置不同的样式

    第一列#id table tr td:first-child{ overflow: visible; }第二列table tr td:first-child+td{color:#666;}第三列tab ...

  2. nginx配置location总结及rewrite规则写法【转】

    转自 nginx配置location总结及rewrite规则写法 | Sean's Noteshttp://seanlook.com/2015/05/17/nginx-location-rewrite ...

  3. 缓存数据库-redis介绍

    一:Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的 ...

  4. JQuery怎么实现页面左侧菜单刷新后保留鼠标点击addclass的样式?

    $('ul.main-menu li a').each(function(){ if($($(this))[0].href==String(window.location)) $(this).pare ...

  5. 网络协议之NAT穿透

    NAT IPv4地址只有32位,最多只能提供大致42.9亿个唯一IP地址,当设备越来越多时,IP地址变得越来越稀缺,不能为每个设备都分配一个IP地址.于是,作为NAT规范就出现了.NAT(Networ ...

  6. (三)Rest风格的资源URL

    第一节:Restful风格的资源URL简介 第二节:SpringMVC对Rest风格的支持 第三节:@PathVariable获取Url变量 第四节:SpringMVC对静态资源的处理 http:// ...

  7. 为什么有些网页QQ浏览器右键没有检查

    改成[极速内核]. 设置->高级设置 改成[极速内核]: 如下图:

  8. MySQL学习笔记:三种组内排序方法

    由于MySQ没有提供像Oracle的dense_rank()或者row_number() over(partition by)等函数,来实现组内排序,想实现这个功能,还是得自己想想办法,最终通过创建行 ...

  9. 接口测试工具--Poster与Postman的简单实用

    HTTP/SOAP协议接口的功能测试: 1.浏览器URL(GET请求) http://127.0.0.1:8000/login/?username=zhangsan&password=1234 ...

  10. PHP性能调优---php-fpm - 启动参数及重要配置详解

    约定几个目录/usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.ini 一,php-fpm ...