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.

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

 

 

建图

所有房子和所有人作为一个点、新增一个源点和一个汇点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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值