HDU 1533Going Home(KM算法求二分图最小权匹配或者最小费用最大流)

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<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>b
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define eps 1e-8
const int INF = 0x3f3f3f3f;
const int N=105;
using namespace std;
int n,m,o;
struct node
{
    int x,y;
}xx[N],yy[N];
char mapp[N][N];
int mp[N][N];
int b1[N],b2[N],v1[N],v2[N],p[N],s[N];
int dfs(int u)
{
    v1[u]=1;
    for(int i=1;i<=o;i++)
    {
        if(v2[i]) continue;
        int gap=b1[u]+b2[i]-mp[u][i];
        if(!gap)
        {
            v2[i]=1;
            if(p[i]==-1||dfs(p[i]))
            {
                p[i]=u;
                return 1;
            }
        }
        else
         s[i]=min(gap,s[i]);
    }
    return 0;
}
int km()
{
    memset(p,-1,sizeof(p));
    memset(b2,0,sizeof(b2));
    for(int i=1;i<=o;i++)
    {
        b1[i]=-INF;
        for(int j=1;j<=o;j++)
            b1[i]=max(b1[i],mp[i][j]);
    }
    for(int i=1;i<=o;i++)
    {
        memset(s,INF,sizeof(s));
        while(1)
        {
            memset(v1,0,sizeof(v1));
            memset(v2,0,sizeof(v2));
            if(dfs(i)) break;
            int d=INF;
            for(int j=1;j<=o;j++)
             if(!v2[j]) d=min(d,s[j]);
             for(int j=1;j<=o;j++)
             {
                 if(v1[j]) b1[j]-=d;
                 if(v2[j]) b2[j]+=d;
                 else s[j]-=d;
             }
        }
    }
    int res=0;
    for(int i=1;i<=o;i++)
     res+=mp[p[i]][i];
    return res;

}
int main()
{
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        int o1=0,o2=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",&mapp[i]);
            for(int j=0;j<m;j++)
            {
                if(mapp[i][j]=='m')
                {
                    xx[++o1].x=i;
                    xx[o1].y=j;
                }
                if(mapp[i][j]=='H')
                {
                    yy[++o2].x=i;
                    yy[o2].y=j;
                }
            }
        }
        o=o1;
        memset(mp,-INF,sizeof(mp));
        for(int i=1;i<=o;i++)
            for(int j=1;j<=o;j++)
                mp[i][j]=-abs(xx[i].x-yy[j].x)-abs(xx[i].y-yy[j].y);
        printf("%d\n",-km());
    }
}
最小费用最大流

PS

这是一个最小费用最大流问题

//最大费用最小流只要在添加边的时候换一下位置就好了
//求最大费用最大流只需要把费用换成相反数,用最小费用最大流求解即可



#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define eps 1e-8
#define ll long long
const int inf = 0x3f3f3f3f;
const long long mod=1e9+7;
const int nMax=1105;
using namespace std;
char s[nMax][nMax];
int cost[nMax][nMax];
int cap[nMax][nMax];
int v[nMax];
int d[nMax];
queue<int>q;
int pre[nMax];
int t,ans,cnt;
struct node
{
    int x,y;
}a1[nMax],a2[nMax];
void end()
{
    for(int i=t;i!=0;i=pre[i])
    {
        cap[pre[i]][i]-=1;
        cap[i][pre[i]]+=1;
    }
    ans+=d[t];
}
int spfa()
{
    memset(v,0,sizeof(v));
    memset(d,inf,sizeof(d));
    d[0]=0;
    v[0]=1;
    q.push(0);
    while(!q.empty())
    {
        int k=q.front();
        q.pop();
        v[k]=0;
        for(int i=0;i<=t;i++)
        {
            if(cap[k][i]&&d[i]>d[k]+cost[k][i])
            {
                d[i]=d[k]+cost[k][i];
                pre[i]=k;
                if(!v[i])
                {
                    v[i]=1;
                    q.push(i);
                }
            }
        }
    }
    if(d[t]==inf) return 0;
    return 1;
}
int main()
{
  int n,m,cnt1,cnt2;
  while(~scanf("%d%d",&n,&m)&&n+m)
  {
      memset(cap,0,sizeof(cap));
      memset(cost,0,sizeof(cost));
      cnt1=cnt2=ans=0;
      for(int i=0;i<n;i++)
      {
          scanf("%s",&s[i]);
          for(int j=0;j<m;j++)
          {
              if(s[i][j]=='H')
                a1[++cnt1].x=i,a1[cnt1].y=j;
              if(s[i][j]=='m')
                a2[++cnt2].x=i,a2[cnt2].y=j;
          }
      }
      cnt=cnt1;
      t=2*cnt+1;
      for(int i=1;i<=cnt;i++)
      {
          cap[0][i]=1;
          cap[i+cnt][t]=1;
          for(int j=1;j<=cnt;j++)
          {
              cap[i][j+cnt]=1;
              cost[i][j+cnt]=abs(a1[i].x-a2[j].x)+abs(a1[i].y-a2[j].y);
              cost[j+cnt][i]-=cost[i][j+cnt];
          }
      }
        while(spfa())
            end();
        printf("%d\n",ans);

  }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值