#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <vector> using namespace std; const int N = + ;
const int oo = 0x3f3f3f3f; struct Edge {
int from, to, cap, flow;
}; struct Dinic {
int n, m, s, t;
int dis[N], cur[N], que[N << ];
bool vis[N];
vector <Edge> edges;
vector <int> G[N]; void add(int from, int to, int cap) {
edges.push_back((Edge) {from, to, cap, });
edges.push_back((Edge) {to, from, , });
m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
} bool bfs() {
int head = , tail = ; memset(vis, false, sizeof vis);
dis[s] = ; vis[s] = true; que[head] = s;
while(head <= tail) {
int x = que[head]; for(int i = ; i < (signed) G[x].size(); ++ i) {
Edge &e = edges[G[x][i]]; if(!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
dis[e.to] = dis[x] + ;
que[++ tail] = e.to;
}
}
++ head;
}
return vis[t];
} int dfs(int x, int a) {
if(x == t || a == ) return a; int flw = , f; for(int &i = cur[x]; i < (signed) G[x].size(); ++ i) {
Edge &e = edges[G[x][i]]; if(dis[e.to] == dis[x] + && (f = dfs(e.to, min(a, e.cap - e.flow))) > ) {
e.flow += f; edges[G[x][i] ^ ].flow -= f; flw += f; a -= f;
if(a == ) break;
}
}
return flw;
} int MaxFlow(int s, int t) {
this->s = s; this->t = t; int flw = ; while(bfs()) {
memset(cur, , sizeof cur);
flw += dfs(s, oo);
}
return flw;
}
}net; int n, M[N]; int main() {
#ifndef ONLINE_JUDGE
freopen("maxflowb.in", "r", stdin);
freopen("maxflowb.out", "w", stdout);
#endif int Up, Down; scanf("%d", &n); net.n = n + ;
for(int i = ; i <= n; ++ i) {
for(int j = ; j <= n; ++ j) {
scanf("%d%d", &Down, &Up);
M[i] -= Down; M[j] += Down;
net.add(i, j, Up - Down);
}
}
net.add(n, , oo);
for(int i = ; i <= n; ++ i) {
if(M[i] > ) {
net.add(, i, M[i]);
}
else if(M[i] < ){
net.add(i, n + , -M[i]);
}
}
net.MaxFlow(, n + );
printf("%d\n", net.MaxFlow(, n)); #ifndef ONLINE_JUDGE
fclose(stdin); fclose(stdout);
#endif
return ;
}

Cogs 12

连边方式见图。

Cogs 12 运输问题2 (有上下界网络流)的更多相关文章

  1. Cogs 12. 运输问题2(有上下界的有源汇最大流)

    运输问题2 ★★☆ 输入文件:maxflowb.in 输出文件:maxflowb.out 简单对比 时间限制:1 s 内存限制:128 MB 运输问题 [问题描述] 一个工厂每天生产若干商品,需运输到 ...

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

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

  3. ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)

    https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...

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

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

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

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

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

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

  7. ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心或有源汇上下界网络流)

    "Oh, There is a bipartite graph.""Make it Fantastic."X wants to check whether a ...

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

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

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

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

  10. 【有上下界网络流】【ZOJ】2314 Reactor Cooling

    [算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...

随机推荐

  1. jQuery自学笔记(四):jQuery DOM节点操作

    获得和设置内容:text( ).html( ) 以及 val( ) text( ) - 设置或返回所选元素的文本内容 html( ) - 设置或返回所选元素的内容(包括 HTML 标记) val( ) ...

  2. jQuery自学笔记(三):jQuery动画效果

    jQuery隐藏和显示: 使用 hide( ) 和 show( ) 方法来隐藏和显示 HTML 元素: 语法: $(selector).hide(speed,callback); $(selector ...

  3. ECharts开源图表使用方法简单介绍

    ECharts图表是基于Canvas,纯Javascript图表库,基于BSD开源协议,官网地址:http://echarts.baidu.com/index.html 需要先下载插件:https:/ ...

  4. js函数的各种写法与调用

    以下是我见过的各种js函数的各种写法以及调用,虽然有些写法及其调用我不清楚其专业术语叫啥,但并不影响我写一个总结笔记. 我们刚开始接触js语音,经常看到的这种名叫“使用function关键字来定义函数 ...

  5. 有n人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来的第几号的那位。

    #include <iostream> using namespace std; int main() { int i,j,n,m,k,*p,num[100];k=m=0;   cin&g ...

  6. iOS学习之数据解析

    解析:按照约定好的格式提取数据的过程叫做解析; 后台开发人员按照约定好的格式存入数据,前端开发人员按照约定的格式读取数据; 主流的格式: XML / JSON 前端和后台都能识别的格式;  XML解析 ...

  7. MVC4 路由参数带点 文件名后缀导致错误

    错误描述 最近在研究office在线预览,用到mvc4  apicontroller 需要传参是文件名,如test.docx导致错误"指定的目录或文件在 Web 服务器上不存在", ...

  8. python 3.5 格式化字符串输出

    #!/usr/bin/env python #encoding: utf-8 #.strip('里面可以去掉字符串中两边的字符') name = input('name :').strip(' ') ...

  9. win7系统64位"禁用驱动程序签名强制"如何设置?

    换了Win7系统64位旗舰版的朋友是不是都发现了一点,以前32位Win7系统用的好好的小程序小应用,一下子就装不上了.这是由于Win7系统64位系统对于未有认证签名的驱动程序进行了限制安装,而大部分小 ...

  10. 用root帐号切换其他帐号提示 su: warning: cannot change directory to /home/oracle: Permission denied

    用root帐号切换其他帐号提示: 出错原因: 基本上是根目录或者是/home/oracle目录权限的问题 解决办法: 更改根目录权限为755,并保证对应用户主目录的所属用户和所属组一致和用户名一致. ...