poj 2195

      这道题有两种方法可以解决,一种是费用流的方法,另一种是二分图最佳匹配的方法。我这里是用费用流的方法。

      简单说一下构图,每个人都与每个房子有一条容量为1的连线,权值就是曼哈顿距离。此外,加上超级源点和超级汇点就行了,连线的容量为1,权值为0。上面说的都是正向边,反向边容量为0,费用是相对应边费用的负值。

      其实求解费用流问题的一个简单思路就是,用单源最短路算法找出一条从源点到汇点的最短路,即代价最小的路径。然后按照最大流的算法去增流,不过要注意,此时计算的不是流量,而是费用,所以要用流量乘以费用。由于会有负权回路,所以最短路算法选spfa算法。下面的代码就是基于上面的思路。顺便说一下,其实也可以用Dijkstra算法,不过需要一些处理。

      代码(C++):

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <queue>

#define MAX 150
#define INF 2000000000
using namespace std;

//#define LOCAL

typedef pair<int,int> pii;
pii array_m[MAX],array_h[MAX];

struct Edge{
    int u;   
    int v;
    int cap;
    int w; 
    int next;  
} edge[MAX*MAX*2+MAX*4];

int head[MAX*2+1],c,dis[MAX*2+1],pre[MAX*2+1];

void add_edge(int u,int v,int cap,int w)
{
    edge[c].u=u; 
    edge[c].v=v;
    edge[c].cap=cap;
    edge[c].w=w;
    edge[c].next=head[u];
    head[u]=c;
    c++; 
}

void spfa(int s)
{
    int i,u,v;
    bool inq[MAX*2+1];
    queue<int> qi;

    for(i=0;i<=MAX*2;i++)
    {
        dis[i]=INF;
        pre[i]=i;
        inq[i]=false;
    }
    dis[s]=0;
    qi.push(s);
    inq[s]=true;
    
    while(!qi.empty())
    {
        u=qi.front();
        qi.pop(); 
        inq[u]=false;
        for(i=head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].v;                               
            if(edge[i].cap>0&&dis[u]+edge[i].w<dis[v])  
            {
                dis[v]=dis[u]+edge[i].w;
                pre[v]=i;
                if(!inq[v])
                {
                   qi.push(v);
                   inq[v]=true;
                }                                                  
            }                             
        }              
    }
}

int add_flow(int s,int u,int f,int &ans)
{
     int e,d;
     if(s==u) return f;
     e=pre[u];
     d=add_flow(s,edge[e].u,min(f,edge[e].cap),ans);
     edge[e].cap-=d;
     edge[e^1].cap+=d;
     ans+=d*edge[e].w;
     return d;
}

int min_cost_f(int s,int t)
{
    int ans;
    ans=0;
    while(1)
    {
        spfa(s);        
        if(dis[t]==INF) return ans;
        add_flow(s,t,INF,ans);    
    }
}

int main(int argc, char *argv[])  //源点:0   汇点:MAX*2   人:1~a-1   房子:1+MAX~b-1+MAX 
{
#ifdef LOCAL
   freopen("in.txt","r",stdin);
   freopen("out.txt","w",stdout);
#endif
    int n,m,i,j,a,b,fee,sum;
    char ch;
    while(scanf("%d %d",&n,&m)&&n+m!=0)
    {
        a=b=1; 
        c=0;
        memset(head,-1,sizeof(head));           
        for(i=0;i<n;i++)   
        {
           getchar();
           for(j=0;j<m;j++)
           {
               scanf("%c",&ch);
               if(ch=='m')
               {
                  add_edge(0,a,1,0);
                  add_edge(a,0,0,0);
                  array_m[a++]=make_pair(i,j);                  
               }else if(ch=='H'){    
                  add_edge(b+MAX,MAX*2,1,0);
                  add_edge(MAX*2,b+MAX,0,0);           
                  array_h[b++]=make_pair(i,j);                  
               }
           }                
        } 
        for(i=1;i<a;i++)
        {
           for(j=1;j<b;j++)
           {
               fee=abs(array_m[i].first-array_h[j].first)+abs(array_m[i].second-array_h[j].second);                        
               add_edge(i,j+MAX,1,fee);
               add_edge(j+MAX,i,0,-fee);            
           }             
        }        
        sum=min_cost_f(0,MAX*2);
        printf("%d\n",sum);         
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

题目( http://poj.org/problem?id=2195):

Going Home
Time Limit: 1000MS Memory Limit: 65536K
   

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值