Cogs 12 运输问题2 (有上下界网络流)
#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 (有上下界网络流)的更多相关文章
- Cogs 12. 运输问题2(有上下界的有源汇最大流)
运输问题2 ★★☆ 输入文件:maxflowb.in 输出文件:maxflowb.out 简单对比 时间限制:1 s 内存限制:128 MB 运输问题 [问题描述] 一个工厂每天生产若干商品,需运输到 ...
- hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )
题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)
https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...
- 算法笔记--最大流和最小割 && 最小费用最大流 && 上下界网络流
最大流: 给定指定的一个有向图,其中有两个特殊的点源S(Sources)和汇T(Sinks),每条边有指定的容量(Capacity),求满足条件的从S到T的最大流(MaxFlow). 最小割: 割是网 ...
- POJ 2396 Budget(有源汇上下界网络流)
Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...
- HDU 4940 Destroy Transportation system(无源汇上下界网络流)
Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph (贪心或有源汇上下界网络流)
"Oh, There is a bipartite graph.""Make it Fantastic."X wants to check whether a ...
- [BZOJ2502]清理雪道 有上下界网络流(最小流)
2502: 清理雪道 Time Limit: 10 Sec Memory Limit: 128 MB Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...
- uoj132/BZOJ4200/洛谷P2304 [Noi2015]小园丁与老司机 【dp + 带上下界网络流】
题目链接 uoj132 题解 真是一道大码题,,,肝了一个上午 老司机的部分是一个\(dp\),观察点是按\(y\)分层的,而且按每层点的上限来看可以使用\(O(nd)\)的\(dp\),其中\(d\ ...
- 【有上下界网络流】【ZOJ】2314 Reactor Cooling
[算法]有上下界网络流-无源汇(循环流) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html //未提交 #include<cstdio> ...
随机推荐
- hibernate像MySQL数据库里面存值是中文乱码的解决合辑
解决办法一: 1.把数据库表每一行改成gb2312_chinese(使用PHPMyAdmin) 2.导入时候还是不行,因此查网上的资料说hibernate持久化层在传输过程中未设置编码格式,这样在 ...
- python比较两个列表
两个列表,随机产生4个不相等的数,计算一下,相同位置上的元素相等的个数,用k1表示. b列表中的元素在a列表中,但位置不相同,有多少个,用k2表示. 例如: a=[0, 4, 7, 3]b=[7, 1 ...
- C语言---return(我的工程笔记本)
迷惑了一天 函数就是输出有问题,进入函数设置密码的时候,当我保存密码准备返回,问题就在此时诞生了,界面是主界面,但功能函数还是没反应,设置序列号初始值的原来按键却还是有反应,莫名其妙,莫名其妙... ...
- Java Tips: 使用Pattern.split替代String.split
String.split方法很常用,用于切割字符串,split传入的参数是正则表达式,它的内部是每次都comiple正则表达式,再调用Pattern.split方法: public String[] ...
- cf E. Dima and Magic Guitar
http://codeforces.com/contest/366/problem/E |x1-x2|+|y1-y2|有四种情况 1.-(x1-x2)+(y1-y2); 2.(x1-x2)-(y1-y ...
- cf B. Maximum Absurdity
http://codeforces.com/contest/332/problem/B #include <cstdio> #include <cstring> #includ ...
- 【转】Android 定时器实现的几种方式和removeCallbacks失效问题详解--不错
原文网址:http://blog.csdn.net/xiaanming/article/details/9011193 实现定时器有很多种方式,在这里我简单的介绍几种方式 (1)使用Handler + ...
- STL中,back_insert_iterator与back_inserter的区别。
1.参考http://www.cplusplus.com网站关于back_insert_iterator与back_inserter的介绍之后,我总算明白了:back_insert_iterator, ...
- ServerSocketChannel实现多Selector高并发server
参考hbase RpcServer,编写了一个简洁版多Selector server,对nio怎么用,Selector如何选择事件会有更深入的认识. client端发送消息:内容长度 + 内容,200 ...
- 使用教程 - BestSync同步软件 - SQL2008R2 数据库定时备份解决方案
需求: 1. 某公司的管理软件,数据库为SQL2008R2.2. 将整个数据库作为一个文件,定时同步到FTP 服务器3. 需要有多个备份,每同步一次,都备份上次的文件到备 ...