2013
11-10

# Going Home

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.

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.

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

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


2
10
28


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main{

static int flow[][];
static int cap[][];
static int cost[][];
static int n ;
public static final int MAX = 1000001;
static int s,t;

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

while(true){
int r = scan.nextInt();
int c = scan.nextInt();
if(r==0&&c==0)
break;

char map[][] = new char[r][c];
n = 0;

for(int i=0;i< r;i++){
map[i] = scan.next().toCharArray();
for(int j=0;j< c;j++){
if(map[i][j]=='H'){
n++;
}
}
}

N h[] = new N[n];
N m[] = new N[n];
int hNum = 0;
int mNum = 0;

for(int i=0;i< r;i++){
for(int j=0;j< c;j++){
if(map[i][j] == 'H')
h[hNum++] = new N(i,j);
if(map[i][j] == 'm')
m[mNum++] = new N(i,j);
}
}
cost = new int[2*n+2][2*n+2];
cap = new int[2*n+2][2*n+2];
flow = new int[2*n+2][2*n+2];

s = 0;
t = 2*n+1;

for(int i=1;i<=n;i++){
cap[s][i] = 1;
cap[i+n][t] = 1;
}
for(int i=0;i< n;i++){
for(int j=0;j< n;j++){
cap[i+1][j+n+1] = 1;
cost[i+1][j+n+1] = Math.abs(m[i].x-h[j].x)+Math.abs(m[i].y-h[j].y);
cost[j+n+1][i+1] = -cost[i+1][j+n+1];
}
}

minCost();

int minCostFlow = 0;

for(int i=1;i<=n;i++){
for(int j=n+1;j<=2*n;j++)
minCostFlow += cost[i][j]*flow[i][j];
}

System.out.println(minCostFlow);
}

}

public static void minCost() {

int dist[] = new int[2*n+2];
int pre[] = new int[2*n+2];
boolean visit[] = new boolean[2*n+2];
Queue< Integer> q = new LinkedList< Integer>();

while(true){
java.util.Arrays.fill(dist, MAX);
java.util.Arrays.fill(pre, -1);
java.util.Arrays.fill(visit, false);

visit[s] = true;
dist[s] = 0;

while(!q.isEmpty()){
int v = q.poll();
visit[v] = false;
for(int i=1;i< 2*n+2;i++){
if(flow[v][i]< cap[v][i]&&dist[i]>dist[v]+cost[v][i]){
dist[i] = dist[v] + cost[v][i];
pre[i] = v;
if(!visit[i]){
visit[i] = true;
}
}
}
}

if(pre[t]==-1)
break;
int minFlow = MAX;
for(int u=t;pre[u]!=-1;u=pre[u])
minFlow = Math.min(minFlow, cap[pre[u]][u]-flow[pre[u]][u]);
for(int u=t;pre[u]!=-1;u=pre[u]){
flow[pre[u]][u] += minFlow;
flow[u][pre[u]] -= minFlow;
}

}

}

}

class N{
int x;
int y;
public N(int x, int y) {
super();
this.x = x;
this.y = y;
}

}

1. /*
* =====================================================================================
*
* Filename: 1366.cc
*
* Description:
*
* Version: 1.0
* Created: 2014年01月06日 14时52分14秒
* Revision: none
* Compiler: gcc
*
* Author: Wenxian Ni (Hello World~), [email protected]
* Organization: AMS/ICT
*
* =====================================================================================
*/

#include
#include

using namespace std;

int main()
{
stack st;
int n,i,j;
int test;
int a[100001];
int b[100001];
while(cin>>n)
{
for(i=1;i>a[i];
for(i=1;i>b[i];
//st.clear();
while(!st.empty())
st.pop();
i = 1;
j = 1;

while(in)
break;
}
while(!st.empty()&&st.top()==b[j])
{
st.pop();
j++;
}
}
if(st.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}