POJ 2195 Going Home(网络流-费用流)

Going Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17777   Accepted: 9059

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

Source

Pacific Northwest 2004

题目大意:

m表示人,H表示房子,它们之间的距离是曼哈顿距离,问你所有人一人个房子的总花费是多少?

解题思路:

用最小费用流即可。构图略。

解题代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

const int maxn=11000;
const int maxm=1100000;
const int inf=(1<<30);

struct edge{
    int u,v,next,f,c;
    edge(int u0=0,int v0=0,int f0=0,int c0=0,int next0=0){
        u=u0,v=v0,f=f0,c=c0,next=next0;
    }
}e[maxm];

int head[maxn],path[maxn],dist[maxn];
bool visited[maxn];
int cnt,src,sink;

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

void adde(int u,int v,int f,int c){
    e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].c=c,e[cnt].next=head[u],head[u]=cnt++;
    e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].c=-c,e[cnt].next=head[v],head[v]=cnt++;
}

bool bfs(){
     for(int i=src;i<=sink;i++){
        dist[i]=inf;
        path[i]=-1;
     }
     dist[src]=0;
     queue <int> q;
     q.push(src);
     visited[src]=true;
     while(!q.empty()){
         int s=q.front();
         q.pop();
         for(int i=head[s];i!=-1;i=e[i].next){
              int d=e[i].v;
              if(e[i].f>0 && dist[s]+e[i].c<dist[d]){
                 dist[d]=dist[s]+e[i].c;
                 path[d]=i;
                 if(!visited[d]){
                    visited[d]=true;
                    q.push(d);
                 }
              }
         }
         visited[s]=false;
     }
     return path[sink]>=0;
}

int getMinCost(){
    int ret=0;
    while(bfs()){
        int delta=inf;
        for(int i=sink;i!=src;i=e[path[i]].u){
            if( e[path[i]].f<delta ) delta=e[path[i]].f;
        }
        for(int i=sink;i!=src;i=e[path[i]].u){
            e[path[i]].f-=delta;
            e[path[i]^1].f+=delta;
        }
        ret+=dist[sink]*delta;
    }
    return ret;
}

int n,m;

void input(){
    init();
    src=0;
    char a[110];
    vector < pair<int,int> > house,man;
    for(int i=0;i<n;i++){
        scanf("%s",&a);
        for(int j=0;j<m;j++){
            if(a[j]=='H') house.push_back(make_pair(i,j));
            else if(a[j]=='m') man.push_back(make_pair(i,j));
        }
    }
    sink=house.size()+man.size()+1;
    for(int i=1;i<=man.size();i++) adde(src,i,1,0);
    for(int i=1;i<=man.size();i++){
        for(int j=1;j<=house.size();j++){
            int tdis=abs(man[i-1].first-house[j-1].first)+abs(man[i-1].second-house[j-1].second);
            adde(i,j+man.size(),1,tdis);
        }
    }
    for(int i=1;i<=house.size();i++) adde(i+man.size(),sink,1,0);
}

void solve(){
    printf("%d\n",getMinCost());
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF && (m||n) ){
        input();
        solve();
    }
    return 0;
}

POJ 2195 Going Home(网络流-费用流)

时间: 2024-10-17 20:18:58

POJ 2195 Going Home(网络流-费用流)的相关文章

POJ训练计划2516_Minimum Cost(网络流/费用流)

解题报告 题意: 有n个商店,m个提供商,k种商品</span> n*k的矩阵,表示每个商店需要每个商品的数目: m*k矩阵,表示每个提供商拥有每个商品的个数 然后对于每个物品k,都有n*m的矩阵 i行j列表示 从j提供商向i商店运送一个k商品的代价是多少 判断所有的仓库能否满足所有客户的需求,如果可以,求出最少的运输总费用 思路: 建图的题,不能直接把所有信息建成图,因为n和m跟k都有关系,如果那样子建图的话,就要把k种拆成m类,每个仓库连向该仓库的第k种,然后再和n连线,有费用, 不过这样

POJ 2195 Going Home(费用流)

http://poj.org/problem?id=2195 题意: 在一个网格地图上,有n个小人和n栋房子.在每个时间单位内,每个小人可以往水平方向或垂直方向上移动一步,走到相邻的方格中.对每个小人,每走一步需要支付1美元,直到他走入到一栋房子里.每栋房子只能容纳一个小人. 计算出让n个小人移动到n个不同的房子需要支付的最小费用. 思路: 源点和每个人相连,容量为1,费用为0. 汇点和每栋房子相连,容量为1,费用为0. 每个人和每栋房子相连,容量为1,费用为人和房子之间的距离. 这样一来,跑一

POJ训练计划2195_Going Home(网络流/费用流)

解题报告 题目传送门 思路: bfs建图跑一下费用流就行. #include <iostream> #include <cstdio> #include <cstring> #include <queue> #define inf 0x3f3f3f3f using namespace std; struct E { int v,cost,cap,next; } edge[100000]; int head[1000],cnt,dis[1000],pre[10

POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次只能向右和向下走,走到一个格子上加上格子的数,可以走k次.问最大的和是多少. 思路: 建图:每个格子掰成两个点,分别叫"出点","入点", 入点到出点间连一个容量1,费用为格子数的边,以及一个容量∞,费用0的边. 同时,一个格子的"出点"向它右.下的格子的"入点"连边,容量∞,费用0. 源点向(0,0)的入点连一个容量K的边,(N-1,N-1)的出点向汇点连一

POJ 2195 Going Home (最小费用最大流)

题目链接:http://poj.org/problem?id=2195 题意:n*m的矩阵,地图上有若干个人(m)和房子(H),且人与房子的数量一致.man每移动一格费用为1,一个房子只能住一个人.现在要求所有的人出发,都入住房子,求最少话费. 思路:建立一个超级源点和汇点,源点与人相连费用为0,容量为1,人与房子相连,费用为人与房子的距离,容量为1,房子与汇点相连,费用为0,容量为1 #include <iostream> #include <cstdlib> #include

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by

POJ 3422 HDU 2686,3376 费用流拆点建图

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3376 http://acm.hdu.edu.cn/showproblem.php?pid=2686 http://poj.org/problem?id=3422 POJ 3422为从矩阵左上角走到右下角,最多走k次,每个格子里的数字只能算一次,后面可以重复经过,求经过的各个数字的和的最大值. 拆点,入点向出点连流量为1,费用为当前格子负值的边,向下方,右方连边,流量为k,费用为0,起点连流量为1,

POJ2135_Farm Tour(网络流/费用流)

解题报告 题目传送门 题意: 一个人有n个农场,他想从1到n去,有从n到1回来,要求路径最短,且没有走重复的路. 思路: 如果两次最短路感觉不行的,可以看成费用流,每一条路容量都是1,这样只要流量等于2就行了. 一次mcmf模版. #include <iostream> #include <cstring> #include <queue> #include <cstdio> #define inf 0x3f3f3f3f using namespace st