Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2313 Accepted Submission(s): 1151
Problem Description
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.
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
题意:在一个n*m的网格,网格上有人和屋子,所有人都要走到屋子里,一间屋子只能容纳一个人。人每次可以走到相邻的格子,花费1美元。问最小花费多少捡钱,能让所有人都进入到屋子里。
思路:建图的时候,将每条边的权值变为负数。然后lx[i]初始化为-INT_MAX,结果输出-ans,就可以得到最小权值。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
#define eps 1e-6
using namespace std;
const int maxn=105;
const int INF=1000000000;
int lx[maxn],match[maxn],ly[maxn],slack[maxn];
int house[maxn][2],man[maxn][2],G[maxn][maxn];
bool visx[maxn],visy[maxn];
char map[maxn][maxn];
int n,row,col;
bool hungary(int u)
{
visx[u]=true;
for(int i=0; i<n; i++)
{
if(visy[i]) continue;
if(lx[u]+ly[i]==G[u][i])
{
visy[i]=true;
if(match[i]==-1||hungary(match[i]))
{
match[i]=u;
return true;
}
}
else slack[i]=min(slack[i],lx[u]+ly[i]-G[u][i]);
}
return false;
}
void KM()
{
int temp;
memset(match,-1,sizeof(match));
for(int i=0;i<n;i++) lx[i]=-INF;
memset(ly,0,sizeof(ly));
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
lx[i]=max(lx[i],G[i][j]);
for(int i=0; i<n; i++)
{
for(int j=0;j<n;j++) slack[j]=INF;
while(1)
{
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(hungary(i)) break;
else
{
temp=INF;
for(int j=0; j<n; j++)
if(!visy[j]&&temp>slack[j]) temp=slack[j];
for(int j=0; j<n; j++)
{
if(visx[j]) lx[j]-=temp;
if(visy[j]) ly[j]+=temp;
else slack[j]-=temp;
}
}
}
}
}
int main()
{
while(scanf("%d%d",&row,&col),row||col)
{
int cnt1=0,cnt2=0;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
cin>>map[i][j];
if(map[i][j]=='m')
{
man[cnt1][0]=i;
man[cnt1++][1]=j;
}
else if(map[i][j]=='H')
{
house[cnt2][0]=i;
house[cnt2++][1]=j;
}
}
n=cnt1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
G[i][j]=-(abs(man[i][0]-house[j][0])+abs(man[i][1]-house[j][1]));
KM();
int ans=0;
for(int i=0; i<n; i++)
ans+=G[match[i]][i];
printf("%d\n",-ans);
}
return 0;
}
费用流AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll __int64
using namespace std;
const int INF = 1000000000;
const int maxn = 205;
struct node{
int x, y;
}man[maxn],house[maxn];
struct Edge{
int u, v, cost, cap, flow, next;
}et[maxn * maxn];
int pre[maxn], dis[maxn], eh[maxn], low[maxn];
int anscost, s, t, num, r, c, m, h;
bool vis[maxn];
char G[105][105];
void init(){
memset(eh, -1, sizeof(eh));
num = 0;
m = h = 0;
}
void add(int u, int v, int cost, int cap, int flow){
Edge e = {u, v, cost, cap, flow, eh[u]};
et[num] = e;
eh[u] = num++;
}
void addedge(int u, int v, int cost, int cap){
add(u, v, cost, cap, 0);
add(v, u, -cost, 0, 0);
}
int getdist(int a, int b){
return abs(man[a].x - house[b].x) + abs(man[a].y - house[b].y);
}
bool spfa(){
queue<int>Q;
fill(&dis[0], &dis[maxn], INF);
memset(vis, false, sizeof(vis));
memset(pre, -1, sizeof(pre));
low[s] = INF, vis[s] = true, dis[s] = 0;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = eh[u]; i != -1; i = et[i].next)
{
int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;
if(cap - flow && dis[v] > dis[u] + cost)
{
dis[v] = dis[u] + cost;
pre[v] = i;
low[v] = min(cap - flow, low[u]);
if(!vis[v])
{
Q.push(v);
vis[v] = true;
}
}
}
}
return dis[t] != INF;
}
void costflow(){
anscost = 0;
while(spfa())
{
int x = pre[t];
anscost += low[t] * dis[t];
while(x != -1)
{
et[x].flow += low[t];
et[x^1].flow -= low[t];
x = pre[et[x].u];
}
}
}
int main()
{
while(scanf("%d%d", &r, &c), r || c)
{
getchar();
init();
for(int i = 0; i < r; i++)
for(int j = 0; j <= c; j++)
{
scanf("%c", &G[i][j]);
if(G[i][j] == 'm')
{
m++;
man[m].x = i, man[m].y = j;
}
else if(G[i][j] == 'H')
{
h++;
house[h].x = i, house[h].y = j;
}
}
s = 0, t = m + h + 1;
for(int i = 1; i <= m; i++) addedge(s, i, 0, 1);
for(int i = 1; i <= h; i++) addedge(m + i, t, 0, 1);
for(int i = 1; i <= m; i++)
for(int j = 1; j <= h; j++)
addedge(i, m + j, getdist(i, j), 1);
costflow();
printf("%d\n", anscost);
}
return 0;
}