贴个板子

#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. hdu_1754I Hate It(线段树)

    hdu_1754I Hate It(线段树) 标签: 线段树 题目链接 题意: 中文题意...不多说了,线段树基础题 直接上代码: #include<cstdio> #include< ...

  2. Linux磁盘使用过载

    系统出现磁盘占用达到100%导致系统崩溃 查看那些个磁盘使用达到100% 命令 df -h  我们发现是www目录使用达到100%,开始清理,首先查找大于100M的大文件 find / -size + ...

  3. Java map 详解 - 用法、遍历、排序、常用API等

    尊重原创: http://www.cnblogs.com/lzq198754/p/5780165.html 概要: java.util 中的集合类包含 Java 中某些最常用的类.最常用的集合类是 L ...

  4. windows下github 出现Permission denied (publickey).解决方法

    今天在学习github的时候遇到了一些问题,然后爬了一会,找到了解决方法记录下来,以防忘记,当然能帮助别人最好啦! github教科书传送门:http://www.liaoxuefeng.com/wi ...

  5. input标签元素,value属性取值问题,赋值

    验证val:<input type="text" id="id" name="name" value="空值"&g ...

  6. Bundle使用&NSBundle

    之 前在初始化一个类的时候:TestViewController *viewcontroller=[[TestViewController alloc]initWithNibName:@"T ...

  7. Centos6.9安装部署nginx服务器

    (一)依赖包安装 首先,gcc,pcre,zlib,openssl安装一边(可以用非-devel,但是嫌麻烦....用非-devel的看这个链接) yum  -y install gcc ------ ...

  8. Java Reflection(getXXX和getDeclaredXXX)

    package com.sunchao.reflection; public class Person { private int age ; private String name; public ...

  9. BIGIP-LTM中的NAT和SNAT

      http://250688049.blog.51cto.com/643101/1095880 一.NAT(Network Address Translation)网络地址转换1.NAT简介 NAT ...

  10. scrapy_数据收集

    什么是数据收集器? 数据以key/value形式存在,收集一些状态,简化数据收集的状态 计算到底发送了多少request等等统计信息 如何对404页面进行设置? 通过response.status等于 ...