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

Going Home HDU - 1533(最小费用最大流)

2017-10-25 00:04 423 查看
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

把问题转化成匹配问题,只不过这个匹配问题又由最小费用最大流解决,和最大流的区别就是最小流的bfs换成了spfa了,其他没什么区别

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=10010;
const int maxx=100100;
const int INF=1<<30;
int to[maxx],nex[maxx],cap[maxx],flow[maxx],cost[maxx];
int head[maxn];
int pre[maxn],dis[maxn];
int eid;
void init()
{
eid=0;
memset(head,-1,sizeof(head));
}
void addEdge(int u,int v,int ca,int co)
{
to[eid]=v;
cap[eid]=ca;
cost[eid]=co;
flow[eid]=0;
nex[eid]=head[u];
head[u]=eid++;

to[eid]=u;
cap[eid]=0;
cost[eid]=-co;
flow[eid]=0;
nex[eid]=head[v];
head[v]=eid++;
}
bool inQ[maxn];
bool spfa(int s,int e,int n)
{
queue<int> que;
memset(inQ,false,sizeof(inQ));
for(int i=0;i<=n;i++)
dis[i]=INF;
dis[s]=0,inQ[s]=true;
pre[s]=-1;
que.push(s);
while(!que.empty())
{
int u=que.front();
que.pop();
inQ[u]=false;
for(int i=head[u];~i;i=nex[i])
{
int v=to[i];
if(cap[i]-flow[i]>0&&dis[v]>dis[u]+cost[i])
{
dis[v]=dis[u]+cost[i];
pre[v]=i;//这是为了修改边的容量
if(!inQ[v])
que.push(v),inQ[v]=true;
}
}
}
return dis[e]!=INF;
}
int minCostMaxFlow(int s,int e,int &minCost,int n)
{
int ans=0;
while(spfa(s,e,n))
{
int mint=INF;
for(int i=pre[e];~i;i=pre[to[i^1]])
{
if(mint>cap[i]-flow[i])
mint=cap[i]-flow[i];
}
ans+=mint;
for(int i=pre[e];~i;i=pre[to[i^1]])
{
flow[i]+=mint;flow[i^1]-=mint;
minCost+=mint*cost[i];
}
}
return ans;
}
int abs(int x)
{
return x>0?x:-x;
}
struct point
{
int x,y;
point(int u,int v):x(u),y(v){}
};
vector<point> H,M;
int main()
{
int n,m;
char c;
while(scanf("%d%d",&n,&m)==2)
{
if(!n&&!m)
break;
H.clear();
M.clear();
init();
scanf("%c",&c);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%c",&c);
if(c=='m')
M.push_back(point(i,j));
else
if(c=='H')
H.push_back(point(i,j));
}
scanf("%c",&c);
}
int h=H.size();
for(int i=0;i<H.size();i++)
{
addEdge(0,i+1,1,0);
for(int j=0;j<M.size();j++)
addEdge(i+1,h+j+1,1,abs(H[i].x-M[j].x)+abs(H[i].y-M[j].y));
}
for(int i=0;i<M.size();i++)
addEdge(H.size()+i+1,H.size()+M.size()+1,1, 0);
int MinCost=0;
minCostMaxFlow(0,H.size()+M.size()+1,MinCost,H.size()+M.size()+1);
printf("%d\n",MinCost);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: