HDU 1533 Going Home 最小費用最大流基礎題
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6764 Accepted Submission(s): 3545
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.
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
建圖
所有房子和所有人作為一個點、新增一個源點和一個匯點s,t
s 向每個 人連一條容量為1,費用為0的邊,每個人向每個房子連一條容量為1,費用為曼哈頓距離的邊,
每個房子向連一條容量為1,費用為0的邊,然後跑mcf
#include <bits/stdc++.h>
using namespace std;
const int maxn = 250 + 100;
const int maxm = 100 + 100;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int,int> P;
//const LL mod = 1e9 + 7;
#define PI 3.1415926
#define sc(x) scanf("%d",&x)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",x)
#define pfln(x) printf("%lld\n",x)
#define pfs(x) printf("%d ",x)
#define rep(n) for(int i = 0; i < n; i++)
#define per(n) for(int i = n-1; i >= 0; i--)
#define mem(a,x) memset(a,x,sizeof(a))
int n,m;
struct edge
{
int from,to,cap,flow,cost;
edge(int from,int to, int cap, int flow, int cost) : from(from),to(to),cap(cap),flow(flow),cost(cost) {}
};
int tot = 0;
vector<edge> E;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void add_edge(int from, int to, int cap, int cost)
{
E.push_back(edge(from,to,cap,0,cost));
E.push_back(edge(to,from,0,0,-cost));
tot = E.size();
G[from].push_back(tot-2);
G[to].push_back(tot-1);
}
bool spfa(int s, int t, int &flow, int &cost)
{
mem(d,INF);
mem(inq,0);
d[s] = 0,inq[s] = 1; p[s] = 0, a[s] = INF;
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();Q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].size(); i++)
{
edge e = E[G[u][i]];
if(e.cap > e.flow && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u],e.cap - e.flow);
if(!inq[e.to]) {Q.push(e.to); inq[e.to] = 1;}
}
}
}
if(d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while(u != s)
{
E[p[u]].flow += a[t];
E[p[u]^1].flow -= a[t];
u = E[p[u]].from;
}
return true;
}
int mcf(int s, int t)
{
int flow = 0,cost = 0;
while(spfa(s,t,flow,cost)) ;
//cout << flow << endl;
return cost;
}
int hx[105],hy[105];
int mx[105],my[105];
int htot = 0, mtot = 0;
void input()
{
getchar();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
char c;
scanf("%c",&c);
if(c == 'H')
hx[++htot] = i,hy[htot] = j;
if(c == 'm')
mx[++mtot] = i,my[mtot] = j;
}
getchar();
}
}
int main()
{
int s,t;
while(scanf("%d%d",&n,&m) && n && m)
{
tot = mtot = htot = 0;
input();
s = 0;t = mtot + htot + 1;
for(int i = 0; i <= t; i++) G[i].clear();
E.clear();
for(int i = 1; i <= mtot; i++)
for(int j = 1; j <= htot; j++)
{
int l = abs(mx[i]-hx[j]) + abs(my[i]-hy[j]);
add_edge(i,j+mtot,1,l);
}
// cout << mtot << endl;
for(int i = 1; i <= mtot; i++)
add_edge(s,i,1,0);
for(int i = 1; i <= htot; i++)
add_edge(i+mtot,t,1,0);
pfn(mcf(s,t));
}
return 0;
}