贴个板子

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=205, M=5e5+5, INF=1e9;
inline ll read(){
char c=getchar();ll x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
} int n, m, u, v, c, b, s, t;
int extra[N];
struct edge{int v, c, f, ne, lower;}e[M];
int cnt=1, h[N];
inline void ins(int u, int v, int c, int b=0) { //printf("ins %d %d %d\n",u,v,c);
e[++cnt]=(edge){v, c, 0, h[u], b}; h[u]=cnt;
e[++cnt]=(edge){u, 0, 0, h[v], b}; h[v]=cnt;
} int q[N], head, tail, d[N], vis[N], cur[N];
bool bfs() {
memset(vis, 0, sizeof(vis));
head=tail=1;
q[tail++]=s; d[s]=0; vis[s]=1;
while(head!=tail) {
int u=q[head++];
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v] && e[i].c > e[i].f) {
vis[e[i].v]=1; d[e[i].v]=d[u]+1;
q[tail++]=e[i].v;
if(e[i].v == t) return true;
}
}
return false;
}
int dfs(int u, int a) {
if(u==t || a==0) return a;
int flow=0, f;
for(int &i=cur[u];i;i=e[i].ne)
if(d[e[i].v] == d[u]+1 && (f=dfs(e[i].v, min(e[i].c-e[i].f, a))) >0) {
flow += f;
e[i].f += f;
e[i^1].f -= f;
a -= f;
if(a==0) break;
}
if(a) d[u]=-1;
return flow;
}
int dinic() {
int flow=0;
while(bfs()) {
for(int i=s; i<=t; i++) cur[i]=h[i];
flow+=dfs(s, INF);
}
return flow;
}
int main() {
freopen("in","r",stdin);
int T=read();
while(T--) {
n=read(); m=read(); s=0; t=n+1;
cnt=1; memset(h,0,sizeof(h)); memset(extra, 0, sizeof(extra));
for(int i=1; i<=m; i++)
u=read(), v=read(), b=read(), c=read(), ins(u, v, c-b, b), extra[u]-=b, extra[v]+=b;
int sum=0;
for(int i=1; i<=n; i++) {
if(extra[i]>0) ins(s, i, extra[i]), sum+=extra[i];
else ins(i, t, -extra[i]);
}
int flow=dinic(); //printf("flow %d %d\n",flow,sum);
if(flow!=sum) puts("NO");
else {
puts("YES");
for(int i=1; i<=m; i++) printf("%d\n",e[i<<1].f + e[i<<1].lower);
}
}
}

ZOJ 2314 Reactor Cooling [无源汇上下界网络流]的更多相关文章

  1. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

  2. ZOJ2314 Reactor Cooling(无源汇上下界可行流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  3. ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...

  4. ZOJ 2314 Reactor Cooling | 无源汇可行流

    题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...

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

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

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

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

  7. ZOJ 2314 Reactor Cooling(无源汇上下界网络流)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题意: 给出每条边流量的上下界,问是否存在可行流,如果存在则输出. ...

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

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

  9. zoj 2314 Reactor Cooling (无源汇上下界可行流)

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

随机推荐

  1. Graph(Floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=4034 Graph Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  2. .NET MongoDB Driver 2.2使用示例

    说明:mongoDBService是对各种常用操作的封装 public class MongoDBService { #region 变量 /// <summary> /// 缓存 /// ...

  3. .NET MongoDB Driver 2.2 API注释

    主要内容 1 MongoClient 1.1构造函数 1.2 方法 2 IMongoDatabase 3 IMongoCollection 4 IMongoCollectionExtensions 5 ...

  4. sass 安装与使用

    1.安装. 安装ruby :http://rubyinstaller.org/downloads 创建项目:在f盘创建一个名为sass的文件夹 ruby命令行:f: ruby命令行:cd sass r ...

  5. 文件末尾判断feof

    feof 作用:如果文件结束,则返回非0值,否则返回0 但要注意的是feof要读取到文件结束标志EOF后,才能判断文件是否结束. 所以使用while(!feof(pFile))会出现最后fread会返 ...

  6. JS使用循环按指定倍数分割数组组成新的数组的方法

    今天一个新人同事问了我一个问题,就是有一个像下边这种不知道具体长度的数组,想以每4个为一组,重新组合为一个二维数组,很简单的需求只需要用到一个循环再去取余数就可以了,写了一个小demo在这里把代码包括 ...

  7. UEP-按钮控制及时间

    按钮的判断 var record = ajaxgrid.getAllRecords(); if(record.length>0){ var isPack=record[0].get(" ...

  8. JavaScript函数声明提升

    首先,JavaScript中函数有两种创建方式,即函数声明.函数表达式两种. 1.函数声明. function boo(){ console.log(123); } boo() 2.函数表达式. va ...

  9. Spring注解依赖注入的三种方式的优缺点以及优先选择

    当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 三种方式的区别小结 ...

  10. Map,List,POJO深拷贝(序列化实现)方法与注意事项

    转载请注明出处,谢谢! 方法1: /*jdk >= 1.5*/ @SuppressWarnings("unchecked") public static <T> ...