elijahqi‘s blog
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
Source
Pacific Northwest 2004
完成下多年前的遗愿 源点向每个人建1的边 每个人向汇建1的边 然后可以向四周建费用1容量inf的边 跑费用流即可
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 11000
using namespace std;
int n,m,num=1,dx[]={0,1,-1,0},dy[]={1,0,0,-1},s,t;
char mp[110][110];
struct node{
int y,z,next,c;
}data[N<<3];
int h[N],flag[N],f[N],pre[N],path[N],id[110][110];
inline void insert1(int x,int y,int z,int c){
data[++num].y=y;data[num].z=z;data[num].c=c;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=0;data[num].c=-c;data[num].next=h[y];h[y]=num;
}
inline bool spfa(){
queue<int>q;memset(f,0x3f,sizeof(f));memset(flag,0,sizeof(flag));f[0]=0;flag[0]=1;memset(pre,-1,sizeof(pre));q.push(0);
while(!q.empty()){
int x=q.front();q.pop();flag[x]=0;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z,c=data[i].c;
if (f[x]+c<f[y]&&z){
f[y]=f[x]+c;pre[y]=x;path[y]=i;
if(!flag[y]) q.push(y),flag[y]=1;
}
}
}if(pre[t]==-1) return 0;else return 1;
}
int main(){
freopen("poj2195.in","r",stdin);
while(1){
scanf("%d%d",&n,&m);if (!n&&!m) return 0;
memset(h,0,sizeof(h));int tot=0;num=1;s=0;t=n*m+1;
for (int i=1;i<=n;++i) scanf("%s",mp[i]+1);
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j) id[i][j]=++tot;
for (int i=1;i<=n;++i){
for (int j=1;j<=m;++j){
if (mp[i][j]=='H') insert1(id[i][j],t,1,0);
if (mp[i][j]=='m') insert1(s,id[i][j],1,0);
for (int k=0;k<4;++k){
int x1=i+dx[k],y1=j+dy[k];if (x1<1||x1>n||y1<1||y1>m) continue;
insert1(id[i][j],id[x1][y1],inf,1);
}
}
}int ans=0;
while(spfa()){
int minn=inf,now=t;
while(now) minn=min(minn,data[path[now]].z),now=pre[now];now=t;
while(now){ans+=data[path[now]].c*minn;data[path[now]].z-=minn;data[path[now]^1].z+=minn;now=pre[now];}
}printf("%d\n",ans);
}
return 0;
}