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

HDU 1533 Going Home

2012-09-17 23:02 218 查看

Going Home

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1799 Accepted Submission(s): 871

[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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>

#define INT_INF 0x3fffffff
#define LL_INF 0x3fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 220
#define E 50000

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;

struct Edge
{
int st,en,cap,flow,cost,next;
} edge[E];
int head
, tot , now
;
int source,sink;
int pre
, dis
;
queue<int> q;
bool vs
;

void add_edge(int st,int en,int cap,int cost)
{
edge[tot].st=st;
edge[tot].en=en;
edge[tot].cap=cap;
edge[tot].flow=0;
edge[tot].cost=cost;
edge[tot].next=head[st];
head[st]=tot++;

edge[tot].st=en;
edge[tot].en=st;
edge[tot].cap=0;
edge[tot].flow=0;
edge[tot].cost=-cost;
edge[tot].next=head[en];
head[en]=tot++;
}

bool SPFA()
{
for(int i=0; i<N; i++)
dis[i]=INT_INF;
memset(vs,0,sizeof(vs));
memset(now,-1,sizeof(now));
while(!q.empty()) q.pop();
q.push(source);
dis[source]=0;
vs[source]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vs[u]=0;
for(int i=head[u],v; i!=-1; i=edge[i].next)
if(edge[i].cap-edge[i].flow>0 && dis[v=edge[i].en]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
now[v]=i;
if(!vs[v])
{
vs[v]=1;
q.push(v);
}
}
}
if(dis[sink]!=INT_INF) return true;
else return false;
}

int MCMF()
{
int cost=0;
while(SPFA())
{
int flow=INT_INF;
for(int u=sink; u!=source; u=edge[now[u]].st)
if(flow>edge[now[u]].cap-edge[now[u]].flow)
flow=edge[now[u]].cap-edge[now[u]].flow;
for(int u=sink; u!=source; u=edge[now[u]].st)
{
edge[now[u]].flow+=flow;
edge[now[u]^1].flow-=flow;
}
cost+=flow*dis[sink];
}
return cost;
}

char s

;
vector<int>x;
vector<int>y;

void build(int n,int m)
{
memset(head,-1,sizeof(head));
tot=0;
source=N-2;
sink=N-1;
for(int i=0; i<n; i++)
scanf("%s",s[i]);
x.clear();
y.clear();
int cnt=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(s[i][j]=='H')
{
add_edge(source,cnt++,1,0);
x.push_back(i*m+j);
}
else if(s[i][j]=='m')
y.push_back(i*m+j);
for(int i=0; i<(int)x.size(); i++)
for(int j=0; j<(int)y.size(); j++)
{
int dis=abs(x[i]/m-y[j]/m)+abs(x[i]%m-y[j]%m);
add_edge(i,j+cnt,1,dis);
}
for(int i=0; i<(int)y.size(); i++)
add_edge(i+cnt,sink,1,0);

}

int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n!=0 || m!=0)
{
build(n,m);
int ans=MCMF();
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: