HDU 1533-Going 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


题意:

     给你一张地图,H代表房子,m代表人,地图中人和房子的数量相等,两者的距离为曼哈顿距离(横坐标和纵坐标之差),问每个人都要跑进房子里的最最小费用。

做法:

     用最小费用最大流的板子就能过,建图是关键,另外KM建立负边也能跑过。


最小费用最大流板子:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=400;
const int maxm=200000;
const int inf=0x3f3f3f3f;
int dis[maxn];
int vis[maxn],pre[maxn];
int head[maxn],cnt;
int n,m,sp,tp;
struct node{
    int to,cap,cost,next;
}e[maxm];
char mp[105][105];
void add(int from,int to,int cap,int cost){
    e[cnt].to=to; e[cnt].cap=cap;
    e[cnt].cost=cost; e[cnt].next=head[from];
    head[from]=cnt++;

    e[cnt].to=from; e[cnt].cap=0;
    e[cnt].cost=-cost; e[cnt].next=head[to];
    head[to]=cnt++;
}
bool spfa(int s,int t,int &flow,int &cost){
    queue<int> q;
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    dis[s]=0;  q.push(s);
    vis[s]=1;
    int d=inf;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(e[i].cap>0&&dis[v]>dis[u]+e[i].cost){
                dis[v]=dis[u]+e[i].cost;
                pre[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(dis[t]==inf){
        return false;
    }
    for(int i=pre[t];~i;i=pre[e[i^1].to]){
        d=min(d,e[i].cap);
    }
    for(int i=pre[t];~i;i=pre[e[i^1].to]){
        e[i].cap-=d;
        e[i^1].cap+=d;
        cost+=e[i].cost*d;
    }
    flow+=d;
    return true;
}
int mcmf(int s,int t){
    int flow=0,cost=0;
    while(spfa(s,t,flow,cost)){
        //cout<<flow<<" "<<cost<<endl;
    }
    return cost;
}
struct Home{
    int x,y;
}H[maxn],P[maxn];
int numH,numP;
void input(){
    for(int i=1;i<=n;i++){
        scanf("%s",mp[i]+1);
        for(int j=1;j<=m;j++){
            if(mp[i][j]=='H'){
                H[++numH].x=i,H[numH].y=j;
            }
            if(mp[i][j]=='m'){
                P[++numP].x=i,P[numP].y=j;
            }
        }
    }
}
void build(){
    for(int i=1;i<=numP;i++){
        for(int j=1;j<=numH;j++){
            int t=abs(P[i].x-H[j].x)+abs(P[i].y-H[j].y);
            add(i,j+numP,1,t);
        }
    }
    for(int i=1;i<=numP;i++){
        add(sp,i,1,0);
    }
    for(int i=numP+1;i<=numH+numP;i++){
        add(i,tp,1,0);
    }
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0) break;
        memset(mp,0,sizeof(mp));
        memset(head,-1,sizeof(head));
        cnt=0,sp=0,numH=0,numP=0;
        input();
        tp=numH+numP+1;
        build();
        printf("%d\n",mcmf(sp,tp));
    }
    return 0;
}

KM板子:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int qwq=0x7fffffff;
int w[1000][1000];  //w数组记录边权值
int line[1000],usex[1000],usey[1000],cx[1000],cy[1000];  //line数组记录右边端点所连的左端点, usex,usey数组记录是否曾访问过,也是判断是否在增广路上,cx,cy数组就是记录点的顶标
int n,ans,m;  //n左m右
char mp[1005][1005];
bool find(int x){
    usex[x]=1;
    for (int i=1;i<=m;i++){
        if ((usey[i]==0)&&(cx[x]+cy[i]==w[x][i])){   //如果这个点未访问过并且它是子图里面的边
            usey[i]=1;
            if ((line[i]==0)||find(line[i])){   //如果这个点未匹配或者匹配点能更改
                line[i]=x;
                return true;
            }
        }
    }
    return false;
}
int km(){
    for (int i=1;i<=n;i++){  //分别对左边点依次匹配
        while (true){
            int d=qwq;
            memset(usex,0,sizeof(usex));
            memset(usey,0,sizeof(usey));
            if (find(i)) break;  //直到成功匹配才换下一个点匹配
            for (int j=1;j<=n;j++){
                if (usex[j]){
                    for (int k=1;k<=m;k++)
                    if (!usey[k]) d=min(d,cx[j]+cy[k]-w[j][k]);  //计算d值
                }
            }
            if (d==qwq) return -1;
            for (int j=1;j<=n;j++)
                if (usex[j]) cx[j]-=d;
            for (int j=1;j<=m;j++)
                if (usey[j]) cy[j]+=d;     //添加新边
        }
    }
    ans=0;
    for (int i=1;i<=m;i++)
    ans+=w[line[i]][i];
    return ans;
}
struct House{
    int x,y;
}H[10005];
struct man{
    int x,y;
}ma[10005];
int numH,numm;
int getdis(int a,int b){
    return abs(H[a].x-ma[b].x)+abs(H[a].y-ma[b].y);
}
int main(){
    int nn,mm;
    while (~scanf("%d%d",&nn,&mm)){
        if(nn==0&&mm==0) break;
        mem(usex,0),mem(usey,0),mem(cy,0);
        numH=0,numm=0;
        for(int i=1;i<=nn;i++){
            scanf("%s",mp[i]+1);
            for(int j=1;j<=mm;j++){
                if(mp[i][j]=='H'){
                    H[++numH].x=i,H[numH].y=j;
                }
                if(mp[i][j]=='m'){
                    ma[++numm].x=i,ma[numm].y=j;
                }
            }
        }
        n=numH,m=numm;
        for(int i=1;i<=numH;i++){
            int d=-qwq;
            for(int j=1;j<=numm;j++){
                w[i][j]=-getdis(i,j);
                d=max(d,w[i][j]);
            }
            cx[i]=d;
        }
        memset(line,0,sizeof(line));
        printf("%d\n",km()*(-1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81700059