POJ 2195 Going Home(费用流或km算法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/81428293

Going Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25067   Accepted: 12560

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

题目大意:有几个房子和几个人,一个房子只能住一个人,一个人只能去一个房子,人去房子的花费为两点间的曼哈顿距离,问最少花费

这道题有费用流和km两种做法,但建图大同小异。对于费用流,我们将房子与源点相连,容量为1,费用为0,人与汇点相连,也是容量为1,费用为0,再把每个房子与人相连,容量为inf,费用为距离,跑费用流就可以了。km的话就是不用源点汇点,直接房子与人连就可以了

费用流代码:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=110;
const int maxm=1e4+7;
const int inf=0x3f3f3f3f;
struct Node
{
    int to;
    int capa;
    int cost;
    int next;
}edge[maxm<<1];
struct A
{
    int x;
    int y;
}home[maxm];
struct B
{
    int x;
    int y;
}man[maxm];
int cnt;
int source,sink;
int head[maxm];
char str[maxn][maxn];
int dis[maxm];
bool vis[maxm];
int pre[maxm];
int rec[maxm];
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
    return;
}
void add(int u,int v,int capa,int cost)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].cost=cost;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].cost=-cost;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(rec,-1,sizeof(rec));
    memset(pre,-1,sizeof(pre));
    memset(vis,false,sizeof(vis));
    queue<int> que;
    dis[source]=0;
    vis[source]=true;
    que.push(source);
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        vis[node]=false;
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa>0&&dis[v]>dis[node]+edge[i].cost)
            {
                dis[v]=dis[node]+edge[i].cost;
                rec[v]=i;
                pre[v]=node;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
    return dis[sink]!=inf;
}
int mcmf()
{
    int maxflow=0;
    int mincost=0;
    while(spfa())
    {
        //cout<<"test"<<endl;
        int flow=inf;
        int node=sink;
        while(node!=source)
        {
            flow=min(flow,edge[rec[node]].capa);
            node=pre[node];
        }
        maxflow+=flow;
        node=sink;
        while(node!=source)
        {
            mincost+=flow*edge[rec[node]].cost;
            edge[rec[node]].capa-=flow;
            edge[rec[node]^1].capa+=flow;
            node=pre[node];
        }
    }
    return mincost;
}
int calc(int nodea,int nodeb)
{
    return abs(home[nodea].x-man[nodeb].x)+abs(home[nodea].y-man[nodeb].y);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(!n&&!m) break;
        init();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str[i]+1);
        }
        int home_cnt=0;
        int man_cnt=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(str[i][j]=='H')
                {
                    home[++home_cnt].x=i;
                    home[home_cnt].y=j;
                }
                if(str[i][j]=='m')
                {
                    man[++man_cnt].x=i;
                    man[man_cnt].y=j;
                }
            }
        }
        for(int i=1;i<=home_cnt;i++)
        {
            for(int j=1;j<=man_cnt;j++)
            {
                add(i,home_cnt+j,inf,calc(i,j));
            }
        }
        source=0;
        sink=home_cnt+man_cnt+2;
        for(int i=1;i<=home_cnt;i++)
        {
            add(source,i,1,0);
        }
        for(int i=1;i<=man_cnt;i++)
        {
            add(home_cnt+i,sink,1,0);
        }
        printf("%d\n",mcmf());
    }
    return 0;
}

km算法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=310;
const int inf=0x3f3f3f3f;
int lena,lenb;
int map[maxn][maxn];
int match[maxn];
int slack[maxn];
int ex_boy[maxn];
int ex_girl[maxn];
bool vis_boy[maxn];
bool vis_girl[maxn];
struct Node
{
    int x,y;
    Node(){}
    Node(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
}home[maxn],man[maxn];
int hn,mn;
int dis(Node a,Node b)
{
    return (abs(a.x-b.x)+abs(a.y-b.y));
}
bool dfs(int node)
{
    vis_girl[node]=true;
    for(int i=1;i<=hn;i++)
    {
        if(vis_boy[i]) continue;
        int gap=ex_boy[i]+ex_girl[node]-map[node][i];
        if(!gap)
        {
            vis_boy[i]=true;
            if(match[i]==-1||dfs(match[i]))
            {
                match[i]=node;
                return true;
            }
        }
        else
        {
            slack[i]=min(slack[i],gap);
        }
    }
    return false;
}
int km()
{
    memset(ex_boy,0,sizeof(ex_boy));
    memset(match,-1,sizeof(match));
    for(int i=1;i<=mn;i++)
    {
        ex_girl[i]=-inf;
        for(int j=1;j<=hn;j++)
        {
            ex_girl[i]=max(ex_girl[i],map[i][j]);
        }
    }
    for(int i=1;i<=mn;i++)
    {
        memset(slack,inf,sizeof(slack));
        while(true)
        {
            memset(vis_girl,false,sizeof(vis_girl));
            memset(vis_boy,false,sizeof(vis_boy));
            if(dfs(i))
            {
                break;
            }
            int d=inf;
            for(int j=1;j<=hn;j++)
            {
                if(!vis_boy[j])
                {
                    d=min(d,slack[j]);
                }
            }
            for(int j=1;j<=mn;j++)
            {
                if(vis_girl[j]) 
                {
                    ex_girl[j]-=d;
                }
            }
            for(int j=1;j<=hn;j++)
            {
                if(vis_boy[j]) 
                {
                    ex_boy[j]+=d;
                }
                else
                {
                    slack[j]-=d;
                }
            }
        }
    }
    int ans=0;
    for(int i=1;i<=hn;i++)
    {
        if(match[i]>-1)
        { 
            ans+=map[match[i]][i];
        }
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        getchar();
        if(!n&&!m) break;
        hn=mn=0;
        char ch;
        for(int i=0;i<n;i++)
        {
            //scanf("%s",ch);
            for(int j=0;j<m;j++)
            {
                //scanf("%c",&ch);
                cin>>ch;
                if(ch=='m')
                {
                    man[++mn]=Node(i,j);
                }
                else if(ch=='H')
                {
                    home[++hn]=Node(i,j);
                }
            }
        }
        for(int i=1;i<=hn;i++)
        {
            for(int j=1;j<=mn;j++)
            {
                map[i][j]=-dis(home[i],man[j]);
            }
        }
        printf("%d\n",-km());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81428293