http://www.artofproblemsolving.com/blog/54262
这个是原作者的博文地址
这是今天刚学的,不过理解上还是很浅薄。最近发现算法不能融会贯通还是因为自己太死板了。
奉上一个基础版本的模板, POJ 2195 的代码。
本模板是不能直接用于任何有负权的图,更不能用于有负圈的情况
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 222
#define MAXM 55555
#define INF 1000000007
using namespace std;
struct EDGE
{
int cost, cap, v;
int next, re;
}edge[MAXM];
int head[MAXN], e;
int vis[MAXN];
int ans, cost, src, des, n;
void init()
{
memset(head, -1, sizeof(head));
e = 0;
ans = cost = 0;
}
void add(int u, int v, int cap, int cost)
{
edge[e].v = v;
edge[e].cap = cap;
edge[e].cost = cost;
edge[e].re = e + 1;
edge[e].next = head[u];
head[u] = e++;
edge[e].v = u;
edge[e].cap = 0;
edge[e].cost = -cost;
edge[e].re = e - 1;
edge[e].next = head[v];
head[v] = e++;
}
int aug(int u, int f)
{
if(u == des)
{
ans += cost * f;
return f;
}
vis[u] = 1;
int tmp = f;
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap && !edge[i].cost && !vis[edge[i].v])
{
int delta = aug(edge[i].v, tmp < edge[i].cap ? tmp : edge[i].cap);
edge[i].cap -= delta;
edge[edge[i].re].cap += delta;
tmp -= delta;
if(!tmp) return f;
}
return f - tmp;
}
bool modlabel()
{
int delta = INF;
for(int u = 1; u <= n; u++)
if(vis[u])
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap && !vis[edge[i].v] && edge[i].cost < delta) delta = edge[i].cost;
if(delta == INF) return false;
for(int u = 1; u <= n; u++)
if(vis[u])
for(int i = head[u]; i != -1; i = edge[i].next)
edge[i].cost -= delta, edge[edge[i].re].cost += delta;
cost += delta;
return true;
}
void costflow()
{
do
{
do
{
memset(vis, 0, sizeof(vis));
}while(aug(src, INF));
}while(modlabel());
}
int nt, m;
struct point
{
int x, y;
}p[MAXN], h[MAXN];
int d[MAXN][MAXN];
char s[MAXN][MAXN];
int main()
{
while(scanf("%d%d", &m, &nt) != EOF)
{
if(m == 0 && nt == 0) break;
for(int i = 0; i < m; i++)
scanf("%s", s[i]);
int hcnt = 0, pcnt = 0;
for(int i = 0; i < m; i++)
for(int j = 0; j < nt; j++)
{
if(s[i][j] == 'H')
{
hcnt++;
h[hcnt].x = i;
h[hcnt].y = j;
}
else if(s[i][j] == 'm')
{
pcnt++;
p[pcnt].x = i;
p[pcnt].y = j;
}
}
for(int i = 1; i <= pcnt; i++)
for(int j = 1; j <= hcnt; j++)
d[i][j] = abs(p[i].x - h[j].x) + abs(p[i].y - h[j].y);
init();
n = hcnt + pcnt + 2;
src = hcnt + pcnt + 1;
des = n;
for(int i = 1; i <= pcnt; i++)
for(int j = 1; j <= hcnt; j++)
add(i, j + pcnt, 1, d[i][j]);
for(int i = 1; i <= pcnt; i++)
add(src, i, 1, 0);
for(int i = 1; i <= hcnt; i++)
add(i + pcnt, des, 1, 0);
costflow();
printf("%d\n", ans);
}
return 0;
}
然后再来一个最基础的模板
这个模板呢,跟上一个一样的地方就是初始化,src代表起点,des是终点,n是顶点的个数
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 555
#define MAXM 55555
#define INF 100000007
using namespace std;
struct EDGE
{
int v, cap, cost, next, re; // re记录逆边的下标。
}edge[MAXM];
int n, m, ans, flow, src, des;
int e, head[MAXN];
int que[MAXN], pre[MAXN], dis[MAXN];
bool vis[MAXN];
void init()
{
e = ans = flow = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int cap, int cost)
{
edge[e].v = v;
edge[e].cap = cap;
edge[e].cost = cost;
edge[e].next = head[u];
edge[e].re = e + 1;
head[u] = e++;
edge[e].v = u;
edge[e].cap = 0;
edge[e].cost = -cost;
edge[e].next = head[v];
edge[e].re = e - 1;
head[v] = e++;
}
bool spfa()
{
int i, h = 0, t = 1;
for(i = 0; i < MAXN; i ++)
{
dis[i] = INF;
vis[i] = false;
}
dis[src] = 0;
que[0] = src;
vis[src] = true;
while(t > h)
{
int u = que[h++];
for(i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if(edge[i].cap && dis[v] > dis[u] + edge[i].cost)
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
que[t++] = v;
}
}
}
vis[u] = false;
}
if(dis[des] == INF) return false;
return true;
}
void end()
{
int u, p, mi = INF;
for(u = des; u != src; u = edge[edge[p].re].v)
{
p = pre[u];
mi = min(mi, edge[p].cap);
}
for(u = des; u != src; u = edge[edge[p].re].v)
{
p = pre[u];
edge[p].cap -= mi;
edge[edge[p].re].cap += mi;
ans += mi * edge[p].cost; // cost记录的为单位流量费用,必须得乘以流量。
}
flow += mi;
}
void build()
{
}
void MCMF()
{
init();
build();
while(spfa()) end();
}
int main()
{
return 0;
}
最后来一个使用 Small Label First 优化
的 SPFA 来维护 zkw 算法中的距离标号, 保留多路增广
这也是根据原作者的程序改的,这个貌似就可以用负的边权了
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 555
#define MAXM 55555
#define INF 100000007
using namespace std;
struct EDGE
{
int cost, cap, v;
int next, re;
}edge[MAXM];
int head[MAXN], e;
int vis[MAXN], d[MAXN];
int ans, cost, src, des, n;
void init()
{
memset(head, -1, sizeof(head));
e = 0;
ans = cost = 0;
}
void add(int u, int v, int cap, int cost)
{
edge[e].v = v;
edge[e].cap = cap;
edge[e].cost = cost;
edge[e].re = e + 1;
edge[e].next = head[u];
head[u] = e++;
edge[e].v = u;
edge[e].cap = 0;
edge[e].cost = -cost;
edge[e].re = e - 1;
edge[e].next = head[v];
head[v] = e++;
}
int aug(int u, int f)
{
if(u == des)
{
ans += cost * f;
return f;
}
vis[u] = 1;
int tmp = f;
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap && !edge[i].cost && !vis[edge[i].v])
{
int delta = aug(edge[i].v, tmp < edge[i].cap ? tmp : edge[i].cap);
edge[i].cap -= delta;
edge[edge[i].re].cap += delta;
tmp -= delta;
if(!tmp) return f;
}
return f - tmp;
}
bool modlabel()
{
for(int i = 0; i <= n; i++) d[i] = INF;
d[des] = 0;
deque<int>Q;
Q.push_back(des);
while(!Q.empty())
{
int u = Q.front(), tmp;
Q.pop_front();
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[edge[i].re].cap && (tmp = d[u] - edge[i].cost) < d[edge[i].v])
(d[edge[i].v] = tmp) <= d[Q.empty() ? src : Q.front()] ? Q.push_front(edge[i].v) : Q.push_back(edge[i].v);
}
for(int u = 1; u <= n; u++)
for(int i = head[u]; i != -1; i = edge[i].next)
edge[i].cost += d[edge[i].v] - d[u];
cost += d[src];
return d[src] < INF;
}
void costflow()
{
while(modlabel())
{
do
{
memset(vis, 0, sizeof(vis));
}while(aug(src, INF));
}
}
int nt, m;
struct point
{
int x, y;
}p[MAXN], h[MAXN];
int dis[MAXN][MAXN];
char s[MAXN][MAXN];
int main()
{
while(scanf("%d%d", &m, &nt) != EOF)
{
if(m == 0 && nt == 0) break;
for(int i = 0; i < m; i++)
scanf("%s", s[i]);
int hcnt = 0, pcnt = 0;
for(int i = 0; i < m; i++)
for(int j = 0; j < nt; j++)
{
if(s[i][j] == 'H')
{
hcnt++;
h[hcnt].x = i;
h[hcnt].y = j;
}
else if(s[i][j] == 'm')
{
pcnt++;
p[pcnt].x = i;
p[pcnt].y = j;
}
}
for(int i = 1; i <= pcnt; i++)
for(int j = 1; j <= hcnt; j++)
dis[i][j] = abs(p[i].x - h[j].x) + abs(p[i].y - h[j].y);
init();
n = hcnt + pcnt + 2;
src = hcnt + pcnt + 1;
des = n;
for(int i = 1; i <= pcnt; i++)
for(int j = 1; j <= hcnt; j++)
add(i, j + pcnt, 1, dis[i][j]);
for(int i = 1; i <= pcnt; i++)
add(src, i, 1, 0);
for(int i = 1; i <= hcnt; i++)
add(i + pcnt, des, 1, 0);
costflow();
printf("%d\n", ans);
}
return 0;
}