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

POJ 2195 Going Home <最小费用最大流>

2017-07-03 21:54 316 查看

题目:

传送门

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

分析:

最小费用最大流基础题。注意题中给出的数据范围是行和列而不是H和m的,另外m是小写,不是大写。这道题我也学着大神们将MCMF封装成类,训练面向对象思维。

代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;

class MCMF{
public:
struct Coor{
int x,y;
Coor(int X,int Y):x(X),y(Y){}
};
MCMF(int _row,int _col):row(_row),col(_col){
input();
initi();
buildG();
solve();
}
void input(){
map=new char*[row];
for(int i=0;i<row;++i){
map[i]=new char[col];
for(int j=0;j<col;++j){
cin>>map[i][j];
if(map[i][j]=='m'){
m.push_back(Coor(i,j));
}
if(map[i][j]=='H')
h.push_back(Coor(i,j));
}
}
n=h.size();
}
void initi(){
minCost=0;
int N=2*n+2;
cost=new int*
;
cap=new int*
;
pre=new int
;
for(int i=0;i<N;++i){
cost[i]=new int
;
cap[i]=new int
;
memset(cost[i],0,sizeof(int)*N);
memset(cap[i],0,sizeof(int)*N);
}
dis=new int
;
vis=new bool
;
s=0; t=N-1;
h.clear(); m.clear();
}
void buildG(){
for(int i=1;i<=n;++i){
cap[s][i]=1;
cap[n+i][t]=1;
for(int j=n+1;j<t;++j){
cap[i][j]=1;
cost[i][j]=abs(m[i-1].x-h[j-n-1].x)+abs(m[i-1].y-h[j-n-1].y);
cost[j][i]=-cost[i][j];
}
}
}
bool SPFA(){
for(int i=1;i<=t;++i){
dis[i]=INF();
vis[i]=false;
}
vis[s]=true; dis[s]=0;
queue<int> q;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=0;i<=t;++i){
if(cap[u][i]&&dis[i]>cost[u][i]+dis[u]){
dis[i]=cost[u][i]+dis[u];
pre[i]=u;
if(!vis[i]){
q.push(i);
vis[i]=true;
}
}
}
}
if(dis[t]==INF()) return false;
return true;
}
void updataG(){
maxFlow=INF();
for(int i=t;i!=s;i=pre[i]){
maxFlow=min(maxFlow,cap[pre[i]][i]);
}
for(int i=t;i!=s;i=pre[i]){
cap[pre[i]][i]-=maxFlow;
cap[i][pre[i]]+=maxFlow;
minCost+=cost[pre[i]][i]*maxFlow;
}
}
void solve(){
while(SPFA()){
updataG();
}
}
int INF(){
return 0x7FFFFFFF;
}
~MCMF(){
cout<<minCost<<endl;
delete[] pre;
delete[] map;
delete[] cost;
delete[] cap;
delete[] dis;
delete[] vis;
}
private:
int row;
int col;
int n;
int s;
int t;
int* pre;
vector<Coor> h;
vector<Coor> m;
char** map;
int** cost;
int** cap;
int* dis;
bool* vis;
int maxFlow;
int minCost;
};

int main(){
int row,col;
while(cin>>row>>col,row||col){
MCMF POJ_2195(row,col);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj