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

hdoj--1533--Going Home(最小费用流)

2016-02-12 15:13 344 查看

Going Home

[align=center]Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3800    Accepted Submission(s): 1948

[/align]

[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

 

[align=left]Recommend[/align]
lwg   |   We have carefully selected several similar problems for you:  1532 3416 3491 3338 3081

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 200+10
#define MAXM 80000+100
#define INF 0x3f3f3f
struct node
{
int from,to,cap,flow,cost,next;
}edge[MAXM];
int head[MAXN],top,pre[MAXN];
int dis[MAXN],vis[MAXN],n,m,source,sink;
void init()
{
top=0;
memset(head,-1,sizeof(head));
}
struct Node
{
int x,y;
};
Node H[MAXN],M[MAXN];
int m_cnt,h_cnt;
void add(int u,int v,int w,int c)
{
node E1={u,v,w,0,c,head[u]};
edge[top]=E1;
head[u]=top++;
node E2={v,u,0,0,-c,head[v]};
edge[top]=E2;
head[v]=top++;
}
int dist(int x1, int y1, int x2, int y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
void getmap()
{
m_cnt=h_cnt=0;
char str[110][110];
for(int i=0;i<n;i++)
{
scanf("%s",str[i]);
for(int j=0;j<m;j++)
{
if(str[i][j]=='m')
{
++m_cnt;
M[m_cnt].x=i;
M[m_cnt].y=j;
}
if(str[i][j]=='H')
{
h_cnt++;
H[h_cnt].x=i;
H[h_cnt].y=j;
}
}
}
int k=m_cnt;
source=0;
sink=2*k+1;
for(int i=1;i<=k;i++)
{
add(source,i,1,0);
add(i+k,sink,1,0);
for(int j=1;j<=k;j++)
{
int d=dist(H[i].x,H[i].y,M[j].x,M[j].y);
add(i,j+k,1,d);
}
}
}
bool SPFA(int s,int t)
{
queue<int>q;
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
dis[s]=0;
vis[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.to]>dis[u]+E.cost&&E.cap>E.flow)
{
dis[E.to]=dis[u]+E.cost;
pre[E.to]=i;
if(!vis[E.to])
{
vis[E.to]=true;
q.push(E.to);
}
}
}
}
return pre[t]!=-1;
}
void MCMF(int s,int t,int &cost,int &flow)
{
flow=0;
cost=0;
while(SPFA(s,t))
{
int Min=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
node E=edge[i];
Min=min(Min,E.cap-E.flow);
}
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
//		printf("%d\n",cost);
}
}
int main()
{
while(scanf("%d%d",&n,&m),n|m)
{
init();
getmap();
int cost,flow;
MCMF(source,sink,cost,flow);
printf("%d\n",cost);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: