您的位置:首页 > 其它

poj 2195 最小费用最大流

2015-08-24 18:46 369 查看
Going Home

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 19759Accepted: 10048
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
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

#pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan
#define clr(s,x) memset(s,x,sizeof(s))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&(-x))
#define PB push_back
#define For(i,a,b) for(int i=a;i<b;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)

typedef long long               LL;
typedef unsigned int            uint;
typedef unsigned long long      ULL;
typedef vector<int>             vint;
typedef vector<string>          vstring;

void RI (int& x){
x = 0;
char c = getchar ();
while (c == ' '||c == '\n')    c = getchar ();
bool flag = 1;
if (c == '-'){
flag = 0;
c = getchar ();
}
while (c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar ();
}
if (!flag)    x = -x;
}
void RII (int& x, int& y){RI (x), RI (y);}
void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);}

const double PI  = acos(-1.0);
const int maxn   = 1e4 + 100;
const int maxm   = 1e3 + 100;
const LL mod     = 1e9 + 7;
const double eps = 1e-9;
const int INF    = 0x7fffffff;

/************************************END DEFINE*********************************************/

struct Side{
int u,v,cap,w;
int nxt;
}side[maxn*6];
int head[maxm],pre[maxm],dis[maxm];
bool vis[maxm];
int min_cost;
int cnt;

struct M{
int x,y;
}man[maxm],house[maxm];
int mn,hn;

void init(){
cnt = 0;
clr(head,-1);
}

void add_side(int u,int v,int cap,int w){
side[cnt].u = u;
side[cnt].v = v;
side[cnt].cap = cap;
side[cnt].w = w;
side[cnt].nxt = head[u];
head[u] = cnt++;

side[cnt].u = v;
side[cnt].v = u;
side[cnt].cap = 0;
side[cnt].w = -w;
side[cnt].nxt = head[v];
head[v] = cnt++;
}

int spfa(int s,int t){
queue<int>q;
while(!q.empty())q.pop();
FOR(i,s,t){
vis[i] = 0;
dis[i] = INF;
pre[i] = -1;
}
dis[s] = 0;
q.push(s);
vis[s] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i=head[u];i!=-1;i=side[i].nxt){
int v = side[i].v;
if(side[i].cap && dis[u]+side[i].w < dis[v]){
dis[v] = dis[u]+side[i].w;
pre[v] = i;
if(!vis[v]){
vis[v] = 1;
q.push(v);
}
}
}
}
if(pre[t] == -1)return 0;
return 1;
}

void End(int t){
int mid = INF;
int k = pre[t];
while(k!=-1){
mid = min(mid,side[k].cap);
k = pre[side[k].u];
}
k = pre[t];
while(k!=-1){
side[k].cap-=mid;
side[k^1].cap+=mid;
k=pre[side[k].u];
}
min_cost += dis[t]*mid;
}

char code[maxm][maxm];

int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0||m==0)break;
init();
int mc=1,hc=1;
FOR(i,1,n){
FOR(j,1,m){
cin>>code[i][j];
if(code[i][j]=='m'){
man[mc].x=i;
man[mc].y=j;
mc++;
}
if(code[i][j]=='H'){
house[hc].x=i;
house[hc].y=j;
hc++;
}
}
}
For(i,1,mc)
add_side(0,i,1,0);
For(i,1,hc)
add_side(mc+i-1,mc+hc-1,1,0);
For(i,1,mc){
For(j,1,hc){
int val = abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y);
add_side(i,mc+j-1,1,val);
}
}
int s = 0,t = mc+hc-1;
min_cost = 0;
while(spfa(s,t))End(t);
printf("%d\n",min_cost);
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: