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

hdoj 1533 Going Home(EK增强版求最小费用最大流)

2016-02-26 19:57 645 查看

Going Home

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

2
10
28


[align=left]Source[/align]
Pacific Northwest 2004

最小费用最大流
最小费用最大流算法其实就是EK增强版,就是把EK中的bfs()找增广路变成spfa()找cost最小的增广路
虽然这题大部分题解是二分图匹配,但还是先学下网络流

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
#define M 40050
#define N 205
#define inf 0x3f3f3f3f
int n,m,cnt,num;
int head
,pre
,preedge
,vis
,dist
;
struct node
{
int u,v,f,cost,next;
}edge[M];
void add(int u,int v,int f,int cost)
{

edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].f=f;
edge[cnt].cost=cost;
edge[cnt].next=head[u];
head[u]=cnt++;

edge[cnt].u=v;
edge[cnt].v=u;
edge[cnt].f=0;
edge[cnt].cost=-cost;
edge[cnt].next=head[v];
head[v]=cnt++;
};
int spfa(int s,int t)//寻找代价最小的增广路
{
int i,now,u,v,cost;
for(i=0;i<num;i++)
{
vis[i]=0;
dist[i]=inf;
pre[i]=-1;
}
queue<int>q;
q.push(s);
dist[s]=0;
vis[s]=1;
while(!q.empty())
{
now=q.front();
q.pop();
vis[now]=0;
for(i=head[now];i!=-1;i=edge[i].next)
{
u=edge[i].u;
v=edge[i].v;
cost=edge[i].cost;
if(edge[i].f>0&&dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
pre[v]=u;
preedge[v]=i;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
return dist[t]<inf;
}
int mincost(int s,int t)
{
int tmp,flow,res,i;
tmp=inf;
flow=0;
res=0;
while(spfa(s,t))//每次增广代价最小的增广路
{
for(i=t;i!=s;i=pre[i])
{
if(tmp>edge[preedge[i]].f)
tmp=edge[preedge[i]].f;
}
for(i=t;i!=s;i=pre[i])
{
edge[preedge[i]].f-=tmp;
edge[preedge[i]^1].f+=tmp;
}
flow+=tmp;
res+=tmp*dist[t];
}
return res;
}
int main()
{
int cnt1,cnt2,i,j,hx
,hy
,mx
,my
;
char s
;
while(scanf("%d%d",&n,&m)&&n+m)
{
cnt1=cnt2=0;
for(i=1;i<=n;i++)
{
scanf("%s",s);
for(j=0;j<m;j++)
{
if(s[j]=='H')
{
hx[++cnt1]=i;
hy[cnt1]=j+1;
}
else if(s[j]=='m')
{
mx[++cnt2]=i;
my[cnt2]=j+1;
}
}
}
num=cnt1*2+2;
cnt=0;
memset(head,-1,sizeof(head));
for(i=1;i<=cnt1;i++)
{
for(j=1;j<=cnt2;j++)
{
int cost=abs(hx[j]-mx[i])+abs(hy[j]-my[i]);
add(i,cnt1+j,1,cost);
}
}
for(i=1;i<=cnt1;i++)
{
add(0,i,1,0);
add(i+cnt1,2*cnt1+1,1,0);
}
printf("%d\n",mincost(0,2*cnt1+1));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: