题意:

给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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ 2391 多源多汇拆点最大流 +flody+二分答案

    题意:在一图中,每个点有俩个属性:现在牛的数量和雨棚大小(下雨时能容纳牛的数量),每个点之间有距离, 给出牛(速度一样)在顶点之间移动所需时间,问最少时间内所有牛都能避雨. 模型分析:多源点去多汇点( ...

  4. POJ 2396 有源有汇有上下界可行流问题

    题意:给一个矩阵,给出每行每列之和,附加一些条件,如第i行第j列数必需大于(小于)多少. 思路题解:矩阵模型,模拟网络流,行.列标号为结点,构图,附加s,t,s连行标(容量上下限每行之和(必需以这个 ...

  5. Wolsey“强整数规划模型”经典案例之一单源固定费用网络流问题

    Wolsey“强整数规划模型”经典案例之一单源固定费用网络流问题 阅读本文可以理解什么是“强”整数规划模型. 单源固定费用网络流问题见文献[1]第13.4.1节(p229-231),是"强整 ...

  6. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  7. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  8. 2018.07.06 POJ 1459 Power Network(多源多汇最大流)

    Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...

  9. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

随机推荐

  1. DTD DOCTYPE

    总结: DOCTYPE是什么 ? 文档类型声明,告诉解析器用什么样的文档类型定义来解析此文档.DOCTYPE不存在或格式不正确会导致文档以兼容模式呈现.   标准模式与兼容模式各有什么区别? 如果页面 ...

  2. [LUOGU] P1113 杂物

    题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务完成是必要的,因为 ...

  3. Python之模块和包导入

    Python之模块和包导入 模块导入: 1.创建名称空间,用来存放模块XX.py中定义的名字 2.基于创建的名称空间来执行XX.py. 3.创建名字XX.py指向该名称空间,XX.名字的操作,都是以X ...

  4. sqlserver常用简单语句

    1.增 插入内容 insert into <表名> (列1,列2,列3) values ('值1','值2','值3') 检索出的内容插入到另外一张表 insert into <表名 ...

  5. Mybatis传递多个参数的4种方式(干货)

    Mybatis传递多个参数的4种方式(干货)-----https://blog.csdn.net/youanyyou/article/details/79406486

  6. 九度oj 题目1070:今年的第几天?

    题目1070:今年的第几天? 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6832 解决:3779 题目描述: 输入年.月.日,计算该天是本年的第几天. 输入: 包括三个整数年(1< ...

  7. bzoj 1430 小猴打架 prufer 性质

    小猴打架 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 709  Solved: 512[Submit][Status][Discuss] Descri ...

  8. 上传文件表单file,限制上传文件类型的方法--参数accept

    我们使用<input type="file" />来上传文件,但是当你只想要上传某种格式的文件,比如说(jpg)文件时.可以通过accept来限制. <form& ...

  9. MU Puzzle HDU - 4662

    Suppose there are the symbols M, I, and U which can be combined to produce strings of symbols called ...

  10. webstrom设置代码缩进四个空格

    言,今天在github上拉了一份代码,发现代码缩进是2个空格,不习惯.找到了解决方案. 一.File ---> Settings 搜索Code Style,取消勾选使用现文件的缩进方式,见下图. ...