您的位置:首页 > 编程语言 > Go语言

Going Home POJ - 2195 最小费用最大流

2017-08-04 10:19 381 查看
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


是一道比较裸的 费用流。做法就是把所有人链接到超级起点容量为1,花费0,把所有房子链接到超级终点容量为1,花费0

然后每个人对每个房子建边花费为坐标距离,容量为1.然后从超级起点跑到超级终点就行了。
#include <cstdio>
#include <queue>
#define min(a,b) (a>b?b:a)
#include<stdlib.h>
#define MAXV 11110
#define MAXM 40010000
#include<string.h>
using namespace std;
typedef struct
{
int s,t,next,w,r;
} Edge;
struct aa
{
int x,y;
} save1[MAXV],save2[MAXV];
Edge edge[MAXM];
int source,sink,n,m,mincost,edgesum;
int head[MAXV],d[MAXV],parent[MAXV],M[MAXV];
char gra[MAXV][MAXV];
void addedge(int a,int b,int c,int r)
{
edge[edgesum].s=a;
edge[edgesum].t=b;
edge[edgesum].r=r;
edge[edgesum].w=c;
edge[edgesum].next=head[a];
head[a]=edgesum++;

edge[edgesum].s=b;
edge[edgesum].t=a;
edge[edgesum].r=0;
edge[edgesum].w=-c;
edge[edgesum].next=head[b];
head[b]=edgesum++;
}

int spfa()
{
queue <int>q;
int v,i,tmp;
bool vis[MAXV];
for(i=0; i<=sink; i++) d[i]=0x3f3f3f3f;
memset(vis,false,sizeof(vis));
memset(parent,-1,sizeof(parent));
memset(M,0x3f3f3f3f,sizeof(M));
q.push(source);
vis[source]=true;
d[source]=0;
while(!q.empty())
{
v=q.front();
q.pop();
vis[v]=false;
for(i=head[v]; i!=-1; i=edge[i].next)
{
tmp=edge[i].t;
if(edge[i].r && edge[i].w+d[v]<d[tmp])
{
d[tmp]=edge[i].w+d[v];
parent[tmp]=i;
M[tmp]=min(M[edge[i].s],edge[i].r);
if(!vis[tmp])
{
q.push(tmp);
vis[tmp]=true;
}
}
}
}
return 0;
}

void MCMF()
{
int u;
mincost=0;
while(1)
{
spfa();
if(parent[sink]==-1) break;

u=parent[sink];
while(u!=-1)
{
edge[u].r-=M[sink];
edge[u^1].r+=M[sink];
u=parent[edge[u].s];
}
mincost+=d[sink];
}
}
int caldis(int x1,int y1,int x2,int y2)
{
return abs(x1-x2)+abs(y1-y2);
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n+m))
{
memset(head,-1,sizeof(head));
edgesum=0,source=0,sink=n+1;
for(int i=1; i<=n; i++)//存图
scanf("%s",gra[i]+1);
int s1=0,s2=0;
for(int i=1; i<=n; i++)//遍历整个图
{
for(int j=1; j<=m; j++)
{
if(gra[i][j]=='m')//记录位置
save1[s1].x=i,save1[s1++].y=j;
if(gra[i][j]=='H')
save2[s2].x=i,save2[s2++].y=j;
}
}
for(int i=0; i<s1; i++)//人与房子建边
{
for(int j=0; j<s2; j++)
{
addedge(i+1,s1+j+1,caldis(save1[i].x,save1[i].y,save2[j].x,save2[j].y),1);
}
addedge(0,i+1,0,1);//人与超级起点
}
for(int i=1; i<=s2; i++)//房子与超级终点
addedge(s1+i,s1+s2+1,0,1);
source=0,sink=s1+s2+1;
MCMF();
printf("%d\n",mincost);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: