最大流模板题
EK
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
#define tt printf("ewgaergasergs\n");
const int N = 10005;
struct node{
int nxt, w, v;
}e[N];
int head[100], re[100], pre[100];
int cnt, n, m;
queue<int> q;
void init()
{
cnt = 0;
memset( head, -1, sizeof( head ));
}
void add( int u, int v, int w)
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].nxt = head[u];
head[u] = cnt++;
e[cnt].v = u;
e[cnt].w = 0;
e[cnt].nxt = head[v];
head[v] = cnt++;
}
int EK()
{
while( !q.empty() )
q.pop();
memset( pre, -1, sizeof( pre ));
q.push(1);
bool OK = 0;
while( !q.empty() )
{
int u = q.front();
q.pop();
for( int i = head[u]; ~i; i = e[i].nxt )
{
int to = e[i].v;
if( pre[to] == -1 && e[i].w > 0 )
{
pre[to] = u;
re[to] = i;
if( to == n )
{
OK = 1;
break;
}
else
q.push(to);
}
}
if( OK )
break;
}
if( !OK )
return 0;
int minflow = 99999999;
int now = n;
//printf("%d\n", pre[1]);
while( 1 )
{
minflow = min( minflow, e[re[now]].w);
now = pre[now];
if( now == 1 )
break;
}
now = n;
while( 1 )
{
e[re[now]].w -= minflow;
e[re[now]^1].w += minflow;
now = pre[now];
if( now == 1 )
break;
}
return minflow;
}
int main()
{
int tot, cas = 1;
scanf("%d\n", &tot);
for( ; cas <= tot; cas++ )
{
init();
scanf("%d%d", &n, &m);
int u, v, w;
while(m--)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, w);
}
int tmp, ans = 0;
while( tmp = EK() )
{
ans += tmp;
}
printf("Case %d: %d\n", cas, ans);
}
return 0;
}
/*
51
4 5
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
*/
SAP:
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 2000;
const int E = 10005;
struct node{
int nxt, v, cap, w;
}e[E];
int n, m, nn;
int st, ed;
int cnt;
int head[N];
int dep[N];
int cur[N];
int gap[N];
int pre[N];
void init()
{
cnt = 0;
memset( head, -1, sizeof( head ) );
st = 1, ed = n, nn = n + 1;
}
void add( int u, int v, int w, int rw = 0 )
{
e[cnt].v = v;
e[cnt].cap = w;
e[cnt].w = 0;
e[cnt].nxt = head[u];
head[u] = cnt++;
e[cnt].v = u;
e[cnt].cap = rw;
e[cnt].w = 0;
e[cnt].nxt = head[v];
head[v] = cnt++;
}
int isap()
{
memset( gap, 0, sizeof( gap ) );
memset( dep, 0, sizeof( dep ) );
memcpy( cur, head, sizeof head );
int u = st;
pre[u] = -1;
gap[0] = nn;
int flow = 0;
while( dep[st] < nn )
{
if( u == ed )
{
int minn = inf, tmp;
for( int i = pre[u]; ~i; i = pre[e[i^1].v] )
{
if( minn > e[i].cap - e[i].w )
{
minn = e[i].cap - e[i].w;
}
}
for( int i = pre[u]; ~i; i = pre[e[i^1].v] )
{
e[i].w += minn;
e[i^1].w -= minn;
}
u = st;
flow += minn;
}
bool f = 0;
int v;
for( int i = cur[u]; ~i; i = e[i].nxt )
{
v = e[i].v;
if( e[i].cap - e[i].w && dep[v]+1 == dep[u] )
{
f = 1;
cur[u] = pre[v] = i;
break;
}
}
if( f )
{
u = v;
continue;
}
int minn = nn;
for( int i = head[u]; ~i; i = e[i].nxt )
{
if( e[i].cap - e[i].w && dep[e[i].v] < minn )
{
cur[u] = i;
minn = dep[e[i].v];
}
}
gap[dep[u]]--;
if( !gap[dep[u]] )
return flow;
dep[u] = minn + 1;
gap[dep[u]]++;
if( u != st )
u = e[pre[u]^1].v;
}
return flow;
}
int main()
{
int tot;
scanf("%d", &tot);
for( int ca = 1; ca <= tot; ++ca )
{
scanf("%d%d", &n, &m);
init();
int u, v, w;
for( int i = 1; i <= m; ++i )
{
scanf("%d%d%d", &u, &v, &w);
add( u, v, w );
}
int ans = isap();
printf("Case %d: ", ca);
printf("%d\n", ans);
}
}