分析:
给出一个网格图,H代表房子,m代表人,两者数量相等,人每移动一格花费1$,求所有人走到房子里所需要的最小花费。利用最小费用最大流来做。(需要注意的是abs函数可能需要自己写)题解:
- 建图:
①从S点到每个人建边,容量为1,费用为0;
②从每个人到每个房子建边,容量为1,费用为曼哈顿距离;
③从每个房子到T建边,容量为1,费用为0。
- 建图:
int NE,head[maxn];
struct Edge
{
int u,v, flow, cost; //flow为流量 cost为费用
int next;
} edge[maxm];
void add(int u,int v,int cap,int cost)
{
edge[NE].u=u;edge[NE].v=v;edge[NE].flow=cap;edge[NE].cost=cost;
edge[NE].next=head[u];head[u]=NE++;
edge[NE].u=v;edge[NE].v=u;edge[NE].flow=0;edge[NE].cost=-cost;
edge[NE].next=head[v];head[v]=NE++;
}
void init()
{
memset(head,-1,sizeof(head));
NE=0;
ans = 0;
}
2.SPFA算法遍历图找最大流最小费用:
bool vis[maxn];
int dist[maxn], pre[maxn];
int s, t, aug;
int n; //记录的是总的边数
bool spfa()
{
int k,p,V;
queue<int> q;
memset(pre, -1,sizeof(pre));
memset(vis, 0, sizeof (vis));
for (int i = 0; i <=n; i++)
dist[i] = INF;
q.push(s);
vis[s] = 1;
dist[s] = 0;
while (!q.empty())
{
k = q.front();
q.pop();
vis[k] = 0;
for (p=head[k]; p!=-1; p=edge[p].next)
{
V = edge[p].v;
if (edge[p].flow && (edge[p].cost + dist[k] < dist[V]))
{
dist[V] = edge[p].cost + dist[k];
pre[V] = p;
if (!vis[V])
{
q.push(V);
vis[V] = 1;
}
}
}
}
if (dist[t] == INF) return false;
aug = INF+1;
for (p=pre[t]; p!=-1; p=pre[edge[p].u])
{
aug = min(aug, edge[p].flow);
}
for (p=pre[t]; p!=-1; p=pre[edge[p].u])
{
edge[p].flow -= aug;
edge[p^1].flow += aug;
}
ans += dist[t] * aug;
return true;
}
//最后计算时这样写
while( spfa());//不断找最短路
printf("%d\n", ans);//找不到就输出
- AC 代码:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cmath>
#define Clear(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn = 101000;
const int maxm = 1000200;;
const int INF = 1000000000;
int absI(int x)
{
if(x<0)
x = -x;
return x;
}
struct Edge
{
int u,v, flow, cost; //flow为流量 cost为费用
int next;
} edge[maxm];
int ans, NE;
int head[maxn];
void add(int u,int v,int cap,int cost)
{
edge[NE].u=u;edge[NE].v=v;edge[NE].flow=cap;edge[NE].cost=cost;
edge[NE].next=head[u];head[u]=NE++;
edge[NE].u=v;edge[NE].v=u;edge[NE].flow=0;edge[NE].cost=-cost;
edge[NE].next=head[v];head[v]=NE++;
}
void init()
{
memset(head,-1,sizeof(head));
NE=0;
ans = 0;
}
bool vis[maxn];
int dist[maxn], pre[maxn];
int s, t, aug;
int n;
bool spfa()
{
int k,p,V;
queue<int> q;
memset(pre, -1,sizeof(pre));
memset(vis, 0, sizeof (vis));
for (int i = 0; i <=n; i++)
dist[i] = INF;
q.push(s);
vis[s] = 1;
dist[s] = 0;
while (!q.empty())
{
k = q.front();
q.pop();
vis[k] = 0;
for (p=head[k]; p!=-1; p=edge[p].next)
{
V = edge[p].v;
if (edge[p].flow && (edge[p].cost + dist[k] < dist[V]))
{
dist[V] = edge[p].cost + dist[k];
//cout << "DIST:" << dist[V] << endl;
pre[V] = p;
if (!vis[V])
{
q.push(V);
vis[V] = 1;
}
}
}
}
//cout << dist[t] << endl;
if (dist[t] == INF) return false;
aug = INF+1;
for (p=pre[t]; p!=-1; p=pre[edge[p].u])
{
aug = min(aug, edge[p].flow);
//cout << aug << endl;
}
for (p=pre[t]; p!=-1; p=pre[edge[p].u])
{
edge[p].flow -= aug;
edge[p^1].flow += aug;
}
ans += dist[t] * aug;
//cout << ans << endl;
return true;
}
struct node
{
int x, y;
};
char Map[110][110];
node man[110];
node House[110];
int main()
{
int nn,mm;
while(scanf("%d%d", &nn, &mm) && (nn+mm) )
{
getchar();
init();
int mans = 0;
int houses = 0;
for(int i=1;i<=nn;i++)
{
for(int j=1;j<=mm;j++)
{
Map[i][j] = getchar();
if(Map[i][j] == 'm')
{
mans++;
man[mans].x = i;
man[mans].y = j;
}
if(Map[i][j] == 'H')
{
houses++;
House[houses].x = i;
House[houses].y = j;
}
}
getchar();
}
s = 0;
t = mans+houses+1;
//cout << t << endl;
n = 0;
for(int i=1; i<=mans; i++)
{
add(s, i, 1, 0);
n++;
}
for(int i=1; i<=mans; i++)
{
for(int j=1; j<=houses; j++)
{
int distant = absI( House[j].x - man[i].x)+absI(House[j].y - man[i].y);
//cout <<"KK "<<distant << endl;
add(i, mans+j, 1, distant);
n++;
}
}
for(int j=1; j<=houses; j++)
{
add(mans+j, t, 1, 0);
n++;
}
ans = 0;
while(spfa() );
printf("%d\n", ans);
}
return 0;
}