HDU1532 最大流-模板题
2013-02-09 08:05
393 查看
题目是网络流-最大流的模板题
这里作为学习,把《指南》上Dinic的模板用了一下,代码如下:
#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
const int MAXN = 10000;
const int INF = 0x3f3f3f3f;
int min(int a, int b){
return a
}
struct Edge{
int from, to, cap, flow;
Edge(int a, int b, int c, int d): from(a), to(b), cap(c), flow(d) {}
};
struct Dinic {
int n, m, s, t;
vector edges;
vectorG[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void AddEdge(){
int from, to, cap;
scanf("%d%d%d", &from, &to, &cap);
Edge tedge = Edge(from, to, cap, 0);
edges.push_back(tedge);
tedge = Edge(to, from, 0, 0);
edges.push_back(tedge);
int m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queueq;
q.push(s);
d[s] = 0;
vis[s] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
for(int i=0; i
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a){
if(x==t || a==0) return a;
int flow = 0, f;
for(int& i=cur[x]; i
Edge& e = edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=DFS(e.to, min(a, e.cap-e.flow))) > 0) {
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(int s, int t){
this->s = s;
this->t = t;
int flow = 0;
while(BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
};
int main(){
// freopen("in.txt", "r", stdin);
int n, m;
while(scanf("%d%d", &n, &m)!=EOF){
int i;
Dinic din;
for(i=0; i
din.AddEdge();
}
printf("%d\n", din.Maxflow(1, m));
}
}
这里作为学习,把《指南》上Dinic的模板用了一下,代码如下:
#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
const int MAXN = 10000;
const int INF = 0x3f3f3f3f;
int min(int a, int b){
return a
}
struct Edge{
int from, to, cap, flow;
Edge(int a, int b, int c, int d): from(a), to(b), cap(c), flow(d) {}
};
struct Dinic {
int n, m, s, t;
vector edges;
vectorG[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void AddEdge(){
int from, to, cap;
scanf("%d%d%d", &from, &to, &cap);
Edge tedge = Edge(from, to, cap, 0);
edges.push_back(tedge);
tedge = Edge(to, from, 0, 0);
edges.push_back(tedge);
int m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queueq;
q.push(s);
d[s] = 0;
vis[s] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
for(int i=0; i
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a){
if(x==t || a==0) return a;
int flow = 0, f;
for(int& i=cur[x]; i
Edge& e = edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=DFS(e.to, min(a, e.cap-e.flow))) > 0) {
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a==0) break;
}
}
return flow;
}
int Maxflow(int s, int t){
this->s = s;
this->t = t;
int flow = 0;
while(BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, INF);
}
return flow;
}
};
int main(){
// freopen("in.txt", "r", stdin);
int n, m;
while(scanf("%d%d", &n, &m)!=EOF){
int i;
Dinic din;
for(i=0; i
din.AddEdge();
}
printf("%d\n", din.Maxflow(1, m));
}
}
相关文章推荐
- HDU-1532 Drainage Ditches (最大流,EK算法模板)
- HDU_1532 && HDU_3549(最大流EK算法模板)
- poj 1273 & hdu 1532 Drainage Ditches(最大流 )EK,dinic模板
- hdu 1532 最大流(模板)
- 【最大流 模板题 EdmondsKarp】HDU - 1532 Drainage Ditches
- 【最大流之EdmondsKarp算法】【HDU1532】模板题
- 【最大流之EdmondsKarp算法】【HDU1532】模板题
- hdu1532 Drainage Ditches网络流——最大流 EK算法模板
- HDU 1532-Drainage Ditches(网络最大流,EK,Ford-Fulkerson,dinic3种算法套模板)
- hdu 1532 Drainage Ditches(最大流 三种模板:EK、Dinic、isap)
- 【最大流之sap】【HDU1532】模板题
- 【最大流之sap】【HDU1532】模板题
- hdu1532最大流EK与SAP算法
- HDU-1532 Drainage Ditches (最大流E-K算法)
- hdu 1532 Drainage Ditches 基础最大流
- hdu 2603 过山车 最大匹配,匈牙利算法模板(易理解)
- HDU 1068 Girls and Boys(模板——二分图最大匹配)
- 文章标题 HDU 3549 : Flow Problem (最大流--模板)
- hdu 3374 next表,最大最小表示法模板
- 最大三角形 HDU - 2202 旋转卡壳模板