您的位置:首页 > 理论基础 > 计算机网络

POJ2195 Going Home(网络流,最小费用流,最小费用路算法)

2018-01-09 11:09 417 查看

Description

On a grid map there are n little men and n houses. In each unit time,

every little man can move one unit step, either horizontally, or

vertically, to an adjacent point. For each little man, you need to pay

a $1 travel fee for every step he moves, until he enters a house. The

task is complicated with the restriction that each house can

accommodate only one little man.

Your task is to compute the minimum amount of money you need to pay in

order to send these n little men into those n different houses. The

input is a map of the scenario, a ‘.’ means an empty space, an ‘H’

represents a house on that point, and am ‘m’ indicates there is a

little man on that point.


You can think of each point on the grid map as a quite large square,

so it can hold n little men at the same time; also, it is okay if a

little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a

line giving two integers N and M, where N is the number of rows of the

map, and M is the number of columns. The rest of the input will be N

lines describing the map. You may assume both N and M are between 2

and 100, inclusive. There will be the same number of ‘H’s and ‘m’s on

the map; and there will be at most 100 houses. Input will terminate

with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is

the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0


Sample Output

2
10
28


思路

题目给了一个n*m的图.其中:

.
代表空地

H
代表房子

m
代表人

人的数量和房子的数量是一样的,现在要让这些人去房子里,每走一步花费的费用为1,问最小要需要多少花费,可以让每个人都进入房子。(房子的容量为1).

首先我们需要建图,把每个人和每个房子的坐标存起来,那么就有l1个人,和l2个房子,建立一个超级源点和超级汇点,给源点和每一个人建立一条容量为1,花费为0的边,给每个房子和汇点建立一个容量为1,花费为0的边。

然后给这些人和这些房子建边。

然后就是最小费用流了

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inf 1000000
#define mem(a,b) memset(a,b,sizeof(a))
const int N=1000+20;
const int M=1000+20;
int top,maxflow;//当前边下标,最大流
int dis
,pre
;//源点到点i的最小距离,pre[i]记录前驱
bool vis
;//标记数组
int first
;//存储头结点
struct Edge
{
int v,next;
int cap,flow,cost;
} E[M*2];

void init()
{
mem(first,-1);
top=0;
maxflow=0;
}
void add_edge(int u,int v,int c,int cost)
{
E[top].v=v;
E[top].cap=c;
E[top].flow=0;
E[top].cost=cost;
E[top].next=first[u];
first[u]=top++;
}
void add(int u,int v,int c,int cost)
{
add_edge(u,v,c,cost);
add_edge(v,u,0,-cost);
}
bool spfa(int s,int t,int n)
{
int i,u,v;
queue<int>q;
mem(vis,false);
mem(pre,-1);
for(int i=1; i<=n; i++) dis[i]=inf;
vis[s]=true;
dis[s]=0;
q.push(s);
while(!q.empty())
{
u=q.front();
q.pop();
vis[u]=false;
for(int i=first[u]; i!=-1; i=E[i].next)
{
v=E[i].v;
if(E[i].cap>E[i].flow&&dis[v]>dis[u]+E[i].cost)
{
dis[v]=dis[u]+E[i].cost;
pre[v]=i;
if(!vis[v])
{
q.push(v);
vis[v]=true;
}
}
}
}
if(dis[t]==inf)
return false;
return true;
}
int MCMF(int s,int t,int n)//minCostMaxFlow
{
int d;
int i,mincost=0;//maxflow当前最大流量,mincost当前最小费用
while(spfa(s,t,n))//表示找到了从s到t的最小费用路
{
d=inf;
for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //遍历反向边
d=min(d,E[i].cap-E[i].flow);
maxflow+=d;//更新最大流
for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //增广路上正向边流量+d,反向边流量-d
{
E[i].flow+=d;
E[i^1].flow-=d;
}
mincost+=dis[t]*d;//dis[t]为该路径上单位流量费用之和
}
return mincost;
}
char s

;
struct node
{
int x,y;
} e1
,e2
;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)&&(n||m))
{
int l1=0,l2=0;
init();
for(int i=0; i<n; i++)
{
scanf("%s",s[i]);
for(int j=0; j<m; j++)
{
if(s[i][j]=='m')
e1[++l1].x=i,e1[l1].y=j;
if(s[i][j]=='H')
e2[++l2].x=i,e2[l2].y=j;
}
}
for(int i=1; i<=l1; i++) add(0,i,1,0);
for(int i=1; i<=l2; i++) add(l1+i,l1+l2+1,1,0);
for(int i=1; i<=l1; i++)
{
for(int j=1; j<=l2; j++)
{
int line=abs(e1[i].x-e2[j].x)+abs(e1[i].y-e2[j].y);
add(i,l1+j,1,line);
}
}
printf("%d\n",MCMF(0,l1+l2+1,l1+l2+2));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: