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 
题意:在一个n*m的二维平面上有x个房子(H),x个人(m),要把x个人安排到x个房子里,一个人可以向四周移动,每移动一步花费一块钱,求将所有人安排完成后,一共最少花多少钱

思路:一对一匹配,求总花费的最小值,kM算法

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f
using namespace std;
const int N = 105;
struct node{
    int x,y;
}H[N],P[N];
int h,p;
int n,m;
int vx[N],vy[N];
int dx[N],dy[N];
int match[N],slack[N];
int mp[N][N];
int nx,ny;
void init(){
    nx=ny=0;
    h=p=0;
    memset(mp,0,sizeof(mp));
}
void getmap(){
    char s[N];
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        for(int j=1;j<=m;j++){
            if(s[j]=='H'){
                H[++h]=(node){i,j};
            }
            else if(s[j]=='m'){
                P[++p]=(node){i,j};
            }
        }
    }
    //将每个房子,每个人分别编号
    for(int i=1;i<=h;i++){ 
        for(int j=1;j<=p;j++){
            int x=abs(H[i].x-P[j].x)+abs(H[i].y-P[j].y);
            mp[i][j]=-x;//将每个人与任意房子的距离的相反数作为权值
        }
    }
    nx=h,ny=p;
}
int Find(int u){
    vx[u]=1;
    for(int i=1;i<=ny;i++){
        if(vy[i]) continue;
        int gap=dx[u]+dy[i]-mp[u][i];
        if(gap==0){
            vy[i]=1;
            if(match[i]==-1||Find(match[i])){
                match[i]=u;
                return 1;
            }
        }
        else slack[i]=min(slack[i],gap);
    }
    return 0;
}
void KM(){
    memset(match,-1,sizeof(match));
    memset(dx,-Inf,sizeof(dx));
    memset(dy,0,sizeof(dy));
    for(int i=1;i<=nx;i++)
    for(int j=1;j<=ny;j++)
       dx[i]=max(dx[i],mp[i][j]);

    for(int i=1;i<=nx;i++){
        memset(slack,Inf,sizeof(slack));
        while(1){
            memset(vx,0,sizeof(vx));
            memset(vy,0,sizeof(vy));

            if(Find(i))break;

            int dis=Inf;

            for(int j=1;j<=ny;j++)
                if(!vy[j]) dis=min(dis,slack[j]);
           for(int j=1;j<=nx;j++){
            if(vx[j]) dx[j]-=dis;
            if(vy[j]) dy[j]+=dis;
            else slack[j]-=dis;
           }
        }
    }
    int ans=0;
    for(int i=1;i<=nx;i++)
        if(match[i]!=-1) ans+=mp[match[i]][i];

    printf("%d\n",-ans);
}
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        if(n==0&&m==0) break;
        init();
        getmap();
        KM();
    }
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电梯调度算法可以采用以下几种: 1. FCFS(First-Come, First-Serve)先来先服务算法:电梯按照请求的顺序进行服务,即按照电梯内部按钮或者外部楼层按钮的按下顺序来进行电梯的运行。 2. SSTF(Shortest-Seek-Time-First)最短寻找时间优先算法:电梯在当前楼层停靠后,选择距离当前楼层最近的楼层进行服务,即选择距离最短的楼层。 3. SCAN算法:电梯在一个方向上,按照楼层从低到高或者从高到低的顺序进行服务,直到到达最顶层或者最底层后,改变方向继续服务。 4. C-SCAN算法:与SCAN算法类似,但是在到达最高层或者最底层后,电梯直接返回到另一端,重新按照方向进行服务。这种算法可以避免电梯在两端徘徊,提高运行效率。 以下是一个简单的电梯调度算法的Java实现(使用FCFS算法): ```java public class Elevator { private int currentFloor; private int[] requests; public Elevator() { currentFloor = 1; requests = new int[10]; } public void addRequest(int floor) { for (int i = 0; i < requests.length; i++) { if (requests[i] == 0) { requests[i] = floor; break; } } } public void run() { for (int i = 0; i < requests.length; i++) { if (requests[i] == 0) { continue; } int floor = requests[i]; if (floor > currentFloor) { System.out.println("Elevator going up to floor " + floor); currentFloor = floor; } else if (floor < currentFloor) { System.out.println("Elevator going down to floor " + floor); currentFloor = floor; } System.out.println("Elevator stopping at floor " + currentFloor); requests[i] = 0; } } } ``` 以上是一个简单的电梯调度算法的Java实现,使用FCFS算法。可以根据需要进行修改和扩展,以适应不同的场景需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值