#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. 聊天室(上篇)GatewayWorker 基础

    前言 本文的目的是基于 GatewayWorker 官方手册,梳理一次 GatewayWorker,并在实践中与 MVC 框架整合的思路(附最终的项目源码).如果你已经理解了整合这一块儿的知识,那么就 ...

  2. 自定义ProgressBar的加载效果

    三种方式实现自定义圆形页面加载中效果的进度条 To get a ProgressBar in the default theme that is to be used on white/light b ...

  3. 十二、springboot之web开发之静态资源处理

    springboot静态资源处理 Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性. 建议大家使用Spring Boot的默 ...

  4. python类中的私有方法

    假设有如下一个python类: class Foo(object): def __a(self): print "Bet you can't see me..." def bar( ...

  5. java 内部类与控制框架

    应用程序控制框架(application framework)就是设计解决某类特殊问题的一个类,或一组类,要运用某个应用程序框架,通常是继承一个类或多个类,并覆盖这些方法.在覆盖的方法中编写代码定制应 ...

  6. C++链接与装载

    1..obj文件的内部结构 2.映射到进程虚拟空间 3.链接的原理    C++ Code  123456789   1.未解决符号表:提供了所有在该编译单元里引用但是定义并不在本编译单元里的符号及其 ...

  7. 第一次ActiveX Fuzzing测试

    接着上一篇的看雪Exploit me试题. 这道题给出了一个ActiveX的DLL,挖掘这个DLL中的漏洞. 由于从来没有接触过ActiveX的Fuzzing,所以找了一些文章来看.自己动手试验了一下 ...

  8. Oracle与Sqlserver数据共享

    需求:在一个集成平台中有一个主系统使用的是Oralce数据库,子系统使用的SqlServer 数据库,如何让子系统的数据库与主系统的人员同步呢? 思路:通过服务WebService 公开接口 1.与主 ...

  9. Spring Boot整合JPA、Redis和Swagger2

    好久没有总结了,最近也一直在学习.今天就把spring boot与其它技术的整合做个小总结,主要是jpa.redis和swagger2.公司里有用到这些,整合起来也很简单. 首先,新建一个Spring ...

  10. jquery图片延迟加载方案解决图片太多加载缓慢问题

    当在做一个图片展示站的时候,一个页面加载的图片过多会,如果服务器的带宽跟不上,明显会感觉到页面很卡,严重的浏览器也会崩溃,所以我推荐采用即看即所得的模式,当滚动到下一屏时才进行加载图片. 注意:即便如 ...