贴个板子

#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. linux基本命令学习01

    =============================================================================Unix/Linux最主要的应用领域是基础服务 ...

  2. cdn和反向代理

    都是用来加速网站访问速度 cdn和反向代理的基本原理都是使用缓存,区别在于cdn部署在网络提供商的机房,使用户在请求网络服务时,可以从距离自己最近的网络提供商机房获取数据:而反向代理则部署在网站的中心 ...

  3. 应用fstream格式化输出

    我举一个我应用的例子 file.open("shoroud.jrf" ,ios_base::trunc); //打开文件,清空文件内容 if(!file.good()) { pri ...

  4. 基于ARM的车牌识别技术研究与实现

    在云盘里包含了我本科毕业设计的全部资料和代码.主要涉及下面摘要中的几个部分.虽然系统无法实用,但是适合机器视觉和嵌入式方向的入门.希望能对有志从事相关方向的朋友有所帮助.本人现在在深圳从事机器视觉算法 ...

  5. load和DOMContenLoaded的区别

    load和DOMContentLoaded的作用就是当页面加载完成的时候自动执行,但他们执行的时间点是不一样的. DOM文档加载步骤: (1)解析html结构 (2)加载外部脚本和样式表文件 (3)解 ...

  6. 将DedeCMS从子目录移动到根目录的方法

    http://www.commonie.com/a/chat/dedeskill/298.html 以前做了一个Wordpress的博客,后来觉得采用DedeCMS更好一点,所以就有了转向DedeCM ...

  7. Oracle实战笔记(第四天)

    导读 今天的主要内容是:两个管理员用户sys&system.数据库的逻辑备份和逻辑恢复.数据字典.表空间&数据文件. 一.Oracle数据库管理员的职责(了解) 数据库管理员(dba) ...

  8. 01 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之业务分析与DAO层

    作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 这是一个整合IDEA+Maven+SSM框架的高并发的商品秒杀项目.我们将分为以下几篇文章来进行详 ...

  9. Flex: Holy Grail

    Flex:Holy Grail <html> <head> <style type="text/css"> body,div,header,ma ...

  10. Mybatis延迟加载和查询缓存

    摘录自:http://www.linuxidc.com/Linux/2016-07/133593.htm 阅读目录 一.延迟加载 二.查询缓存 一.延迟加载 resultMap可以实现高级映射(使用a ...