poj 1459 多源汇网络流 ISAP
题意:
给n个点,m条边,有np个源点,nc个汇点,求最大流
思路:
超级源点把全部源点连起来。边权是该源点的最大同意值;
全部汇点和超级汇点连接起来,边权是该汇点的最大同意值。
跑最大流
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
using namespace std; #define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a)) typedef pair<int,int> pii;
typedef long long LL;
//------------------------------
const int maxn = 205;
const int maxm = 205 * 205;
const int max_nodes = maxn; struct Edge{
int from, to;
int capacity, flow;
Edge(){}
Edge(int from_, int to_, int capacity_, int flow_){
from = from_;
to = to_;
capacity = capacity_;
flow = flow_;
}
};
Edge edges[maxm];
int cnte;
vector<int> g[maxn];
void graph_init(){
cnte = 0;
for(int i = 0; i < maxn; i++) g[i].clear();
} void add_Edge(int from, int to, int cap){
edges[cnte].from = from;
edges[cnte].to = to;
edges[cnte].capacity = cap;
edges[cnte].flow = 0;
g[from].push_back(cnte);
cnte++;
edges[cnte].from = to;
edges[cnte].to = from;
edges[cnte].capacity = 0;
edges[cnte].flow = 0;
g[to].push_back(cnte);
cnte++;
} int source; // 源点
int sink; // 汇点
int p[maxn]; // 可增广路上的上一条弧的编号
int num[maxn]; // 和 t 的最短距离等于 i 的节点数量
int cur[maxn]; // 当前弧下标
int d[maxn]; // 残量网络中节点 i 到汇点 t 的最短距离
bool visited[maxn];
int num_nodes, num_edges; void bfs(){ // 预处理, 反向 BFS 构造 d 数组
memset(visited, 0, sizeof(visited));
queue<int> q;
q.push(sink);
visited[sink] = true;
d[sink] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
for(int i = 0; i < g[u].size(); i++){
Edge& e = edges[g[u][i] ^ 1];
int v = e.from;
if(!visited[v] && e.capacity > e.flow){
visited[v] = true;
d[v] = d[u] + 1;
q.push(v);
}
}
}
}
int augment(){ // 增广
int u = sink, df = INF;
// 从汇点到源点通过 p 追踪增广路径, df 为一路上最小的残量
while(u != source){
Edge& e = edges[p[u]];
df = min(df, e.capacity - e.flow);
u = e.from;
}
u = sink;
// 从汇点到源点更新流量
while(u != source){
Edge& e = edges[p[u]];
e.flow += df;
edges[p[u]^1].flow -= df;
u = e.from;
}
return df;
}
int max_flow(){
int flow = 0;
bfs();
memset(num, 0, sizeof(num));
for(int i = 0; i < num_nodes; i++) num[d[i]] ++; // 这个地方,针对的是点从0開始编号的情况
int u = source;
memset(cur, 0, sizeof(cur));
while(d[source] < num_nodes){
if(u == sink){
flow += augment();
u = source;
}
bool advanced = false;
for(int i = cur[u]; i < g[u].size(); i++){
Edge& e = edges[g[u][i]];
if(e.capacity > e.flow && d[u] == d[e.to] + 1){
advanced = true;
p[e.to] = g[u][i];
cur[u] = i;
u = e.to;
break;
}
}
if(!advanced){ //retreat
int m = num_nodes - 1;
for(int i = 0; i < g[u].size(); i++){
Edge& e = edges[g[u][i]];
if(e.capacity > e.flow) m = min(m, d[e.to]);
}
if(--num[d[u]] == 0) break; // gap 优化
num[d[u] = m+1] ++;
cur[u] = 0;
if(u != source){
u = edges[p[u]].from;
}
}
}
return flow;
}
//------------------------我是分界线,上面是模板--------------------------
int np, nc, n, m; void solve(){
graph_init();
int u, v, w;
char ch;
for(int i = 1; i <= m; i++){
while(scanf("%c", &ch)){
if(ch == '(') break;
}
scanf("%d,%d%c%d",&u,&v,&ch,&w);
add_Edge(u, v, w);
}
for(int i = 1; i <= np; i++){
while(scanf("%c", &ch)){
if(ch == '(') break;
}
scanf("%d%c%d",&u, &ch, &w);
add_Edge(n, u, w);
}
for(int i = 1; i <= nc; i++){
while(scanf("%c", &ch)){
if(ch == '(') break;
}
scanf("%d%c%d",&v, &ch, &w);
add_Edge(v, n+1, w);
} num_nodes = n+2;
source = n; sink = n+1; int ans = max_flow();
printf("%d\n",ans);
}
int main(){
while(scanf("%d%d%d%d",&n,&np, &nc, &m) != EOF){
solve();
}
return 0;
}
最终自己写了一个还不错的最大流啦....窝原来用的那个模板我囧的非常好了啊!
可是我干囧大家好像都再说Dinic是好。又有人在说ISAP好像更棒啊!
最终我如今两个都敲一下就好啦~~
ISAP跟DINIC还是有非常多相似的。
。
poj 1459 多源汇网络流 ISAP的更多相关文章
- poj 1459 多源多汇点最大流
Sample Input 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 ...
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- POJ 2391 多源多汇拆点最大流 +flody+二分答案
题意:在一图中,每个点有俩个属性:现在牛的数量和雨棚大小(下雨时能容纳牛的数量),每个点之间有距离, 给出牛(速度一样)在顶点之间移动所需时间,问最少时间内所有牛都能避雨. 模型分析:多源点去多汇点( ...
- POJ 2396 有源有汇有上下界可行流问题
题意:给一个矩阵,给出每行每列之和,附加一些条件,如第i行第j列数必需大于(小于)多少. 思路题解:矩阵模型,模拟网络流,行.列标号为结点,构图,附加s,t,s连行标(容量上下限每行之和(必需以这个 ...
- Wolsey“强整数规划模型”经典案例之一单源固定费用网络流问题
Wolsey“强整数规划模型”经典案例之一单源固定费用网络流问题 阅读本文可以理解什么是“强”整数规划模型. 单源固定费用网络流问题见文献[1]第13.4.1节(p229-231),是"强整 ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- 2018.07.06 POJ 1459 Power Network(多源多汇最大流)
Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
随机推荐
- pycharm debug后会出现 step over /step into/step into my code /force step into /step out 分别表示
1.debug,全部打印 2.打断点debug,出现单步调试等按钮,只运行断点前 3.setup over 调试一行代码 4.setup out 运行断点后面所有代码 5.debug窗口显示调试按钮 ...
- PHP项目中配置Apache环境
安装Apache服务器(PHP环境) 首先应该去官网上下载响应的压缩包文件,此时应该注意自己电脑所安装的VC依赖包版本,应该下载对应依赖包的压缩包,且应该根据自己系统的版本选择64或32位压缩包,目前 ...
- tcpdump用于抓取tcp数据包
一.简单使用:-c监听次数.-v打印详情.host后接监听地址 1.1.监听 tcpdump -c -v host www.baidu.com 1.2.访问被监听的网址: 1.3.查看监听的数据:
- ruby on rails安装(win7x64)
Ruby下载地址http://rubyinstaller.org/downloads/ (以安装2.1.7为例,2.2.3未能安装成功) 安装完之后测试是否安装成功
- npm run build 打包后,如何查看效果
我们用vue-cli搭建的项目执行npm build后本地打开页面空白,如果才能查看npm run build之后的结果呢 首先我们看一下提示 Tip: built files are meant t ...
- 解决EF 4.0 中数据缓存机制
EF4.0默认开启缓存机制,如果想要禁用缓存机制的话,则须加上一句话:_db.CreateObjectSet().MergeOption = MergeOption.OverwriteChanges; ...
- JavaEE JDBC 事务
JDBC 事务 @author ixenos 事务 1.概念:我们将一组语句构建成一个事务(trans action),当所有语句顺利执行之后,事务可以被提交(commit):否则,如果其中某个语句遇 ...
- 完美解决了span的宽度设置
下 面代码的CSS定义完美解决了span的宽度设置问题.由于浏览器通常对不支持的CSS属性采取忽略处理的态度,所以最好将display:inline -block行写在后面,这样在Firefox里面, ...
- Bzoj3038 上帝造题的七分钟2 线段树
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1135 Solved: 509 Description XLk觉得<上帝造题的七分钟>不太 ...
- tyvj1045 最大的算式
描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号.例 ...