POJ 2195 Going Home [二分图带权匹配] [费用流]

8 篇文章 0 订阅
7 篇文章 0 订阅

Going Home
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

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


二分图带权匹配裸题。
原本以为对于稠密图网络流算法会比KM算法慢很多的,但事实上94ms就过了。。。
(其实KM只要0ms -_-)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 105;
struct Edge
{
    int to,next;
    int val,cap;
}edge[maxn*maxn<<1]; // provided at most 100 houses
int head[maxn<<1];
int maxedge;
inline void addedge(int u,int v,int val,int cap)
{
    edge[++maxedge] = (Edge) { v,head[u],val,cap };
    head[u] = maxedge;
    edge[++maxedge] = (Edge) { u,head[v],-val,0 };
    head[v] = maxedge;
}
int row,col;
int man[maxn][2],house[maxn][2];
inline int cost(int i,int j)
{
    return abs(man[i][0]-house[j][0])+abs(man[i][1]-house[j][1]);
}
int totm,toth;
int S,T;
bool init()
{
    if(!~scanf("%d%d",&row,&col) || !row ||!col) return false;
    memset(head,-1,sizeof(head));
    maxedge = -1;
    totm=toth=0;
    char ch;
    for(int i=1;i<=row;i++)
        for(int j=1;j<=col;j++)
        {
            ch=getchar();
            while(ch^'.' && ch^'H' && ch^'m') ch=getchar();
            if(ch=='m') man[++totm][0]=i,man[totm][1]=j;
            if(ch=='H') house[++toth][0]=i,house[toth][1]=j;
        }
    for(int i=1;i<=totm;i++)
        for(int j=1;j<=toth;j++)
            addedge(i,j+totm,cost(i,j),1);
    S=totm+toth+1; T=S+1;
    for(int i=1;i<=totm;i++) addedge(S,i,0,1);
    for(int j=1;j<=toth;j++) addedge(j+totm,T,0,1);
    return true;
}
bool insta[maxn<<1];
int dis[maxn<<1],pre[maxn<<1];
bool spfa()
{
    queue <int> que;
    memset(pre,-1,sizeof(pre));
    memset(dis,0x3f,sizeof(dis));
    que.push(S); insta[S]=true; dis[S]=0;
    while(!que.empty())
    {
        int u=que.front(); que.pop(); insta[u]=false;
        for(int i=head[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(!edge[i].cap) continue;
            if(dis[v]>dis[u]+edge[i].val)
            {
                dis[v]=dis[u]+edge[i].val;
                pre[v]=i;
                if(insta[v]) continue;
                insta[v]=true;
                que.push(v);
            }
        }
    }
    return dis[T]^INF;
}
int MCMF()
{
    int cost=0;
    while(spfa())
    {
        int minf=INF;
        for(int i=pre[T];~i;i=pre[edge[i^1].to]) smin(minf,edge[i].cap);
        cost += minf*dis[T];
        for(int i=pre[T];~i;i=pre[edge[i^1].to])
            edge[i].cap-=minf,edge[i^1].cap+=minf;
    }
    return cost;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("home.in","r",stdin);
    freopen("home.out","w",stdout);
    #endif
    while(init())
        printf("%d\n",MCMF());
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值