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 ...
随机推荐
- python 深复制和浅复制
https://www.python-course.eu/python3_deep_copy.php-------------------------------------------------- ...
- spring封装的RabbitMQ
spring这么牛逼的团队,封装了RabbitMQ,简化了RabbitMQ的使用,那肯定是要使用spring-rabbit了 一.简介 二.使用方法 1.消费者 public class Foo { ...
- BZOJ 3158 千钧一发 最小割
分析: 偶数对满足条件2,所有奇数对满足条件1. 如果你能一眼看出这个规律,这道题就完成了一半. 我们只需要将数分为两类,a值为奇数,就从S向这个点连容量为b值的边,a值为偶数,就从这个点向T连容量为 ...
- 字符与数字的转换:sprintf和sscanf
目录 字符与数字的转换:sprintf和sscanf 简单介绍 实例 运行结果 字符与数字的转换:sprintf和sscanf 简单介绍 sprintf和sscanf都是stdio.h头文件中的函数, ...
- TensorFlow2-维度变换
目录 TensorFlow2-维度变换 Outline(大纲) 图片视图 First Reshape(重塑视图) Second Reshape(恢复视图) Transpose(转置) Expand_d ...
- python_流程控制
1.if...else 语句 单分支 if 条件: 满足条件后要执行的代码 双分支: """ if 条件: 满足条件执行代码 else: if条件不满足就走这段 & ...
- ppt_旋转抽奖_制作步骤
1 ppt制作抽奖转盘 插入饼状图,更改比例,更改颜色(一般选取相邻的一组颜色匹配比较好看): 插入竖文本框,编辑每一个部分为特等奖.一等奖.二等奖.三等奖: 全部选中,ctrl+G组合: 添加陀螺旋 ...
- noip模拟赛 Nephren Ruq Insania
题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg 注意:本题大样例4的输出文件修改为 https://pan.baidu.com/s/1b ...
- Github上600多个iOS开源项目分类及介绍
将Github上600多个iOS开源项目进行分类并且有相应介绍,小伙伴们快来看呀 地址:http://github.ibireme.com/github/list/ios/
- 【BZOJ4710】分特产(容斥原理,组合计数)
题意:有m种特产,第i种有a[i]个 有n个同学分特产,要求: 1.恰好分完 2.每个人至少要分到一个 求方案数模10^9+7 n,m,a[i]<=1000 思路:WYZ作业 首先考虑对于每一种 ...