hdu - 3549 Flow Problem (最大流模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=3549

Ford-Fulkerson算法.

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std; struct edge
{
int to,cap,rev;
edge(){}
edge(int x,int y,int z)
{
to=x;
cap=y;
rev=z;
}
}; vector<edge>G[maxv];
bool used[maxv]; void add_edge(int from,int to,int cap)
{
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,,G[from].size()-});
} int dfs(int v,int t,int f)
{
if(v==t) return f;
used[v]=true;
for(int i=;i<G[v].size();i++)
{
edge &e=G[v][i];
if(!used[e.to]&&e.cap>)
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
// printf("%d %d\n",e.to,e.rev);
return d;
}
}
}
return ;
} int max_flow(int s,int t)
{
int flow=;
for(;;)
{
memset(used,,sizeof(used));
int f=dfs(s,t,inf);
if(f==) return flow;
flow+=f;
}
}
int main()
{
//freopen("a.txt","r",stdin);
int t,n,m,a,b,c,j=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
G[i].clear();
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
printf("Case %d: %d\n",j++,max_flow(,n));
}
return ;
}

Dinic算法:(动态邻接表)

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std; struct edge
{
int to,cap,rev;
edge(){}
edge(int x,int y,int z)
{
to=x;
cap=y;
rev=z;
}
}; vector<edge>G[maxv];
int level[maxv];
int iter[maxv]; void add_edge(int from,int to,int cap)
{
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,,G[from].size()-});
} void bfs(int s)
{
memset(level,-,sizeof(level));
queue<int>que;
level[s]=;
que.push(s);
while(!que.empty())
{
int v=que.front();que.pop();
for(int i=;i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[v]+;
que.push(e.to);
}
}
}
}
int dfs(int v,int t,int f)
{
if(v==t) return f;
for(int &i=iter[v];i<G[v].size();i++)
{
edge &e=G[v][i];
if(e.cap>&&level[v]<level[e.to])
{
int d=dfs(e.to,t,min(f,e.cap));
if(d>)
{
e.cap-=d;
G[e.to][e.rev].cap+=d;
// printf("%d %d %d\n",e.to,e.rev,G[e.to][e.rev]);
return d;
}
}
}
return ;
} int max_flow(int s,int t)
{
int flow=;
for(;;)
{
bfs(s);
if(level[t]<) return flow;
memset(iter,,sizeof(iter));
int f;
while((f=dfs(s,t,inf))>)
flow+=f;
}
}
int main()
{
//freopen("a.txt","r",stdin);
int t,n,m,a,b,c,j=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
G[i].clear();
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
add_edge(a,b,c);
}
printf("Case %d: %d\n",j++,max_flow(,n));
}
return ;
}
上一篇:hdu 3549 Flow Problem (最大流)


下一篇:Unobtrusive Ajax