HDUOJ 2195 二分图最优匹配

本文介绍了一个经典的最优匹配问题,通过寻找每个人到每个房子的最短路径,实现所有人进入不同房子的最低成本。使用了匈牙利算法进行求解。
Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17793 Accepted: 9070

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


题意:有n个人n个房子,开始时每个人都不在房子内,让每个人走一步需要消耗1元,每个房子里只能装一个人,人可以站在房子所在的点但不进入房子,也可以很多人站在一个点,要求让所有人都进到房子里的最小花费。


思路:求出每个人到每个房子的花费,令X集合为所有人的集合,Y集合为所有房子的集合,原问题就转化成了二分图的最优匹配。(坑爹的变量名居然和math库冲突了。。。)


代码:


#include<iostream>
#include<cstdio>
//#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<ctype.h>
#include<bitset>
#include<map>
# pragma comment (linker,"/STACK:16777216")

using namespace std;

const int MAXN = 205;
const int INF  = 2100000000;
const double esp = 1e-9;

class Point
{
    public:
    int row, col;
};
Point man[MAXN];
Point house[MAXN];

int xn, yn;
int n, m;
int mat[MAXN][MAXN];
bool visx[MAXN], visy[MAXN];
int slack[MAXN];
int link[MAXN];
int lx[MAXN], ly[MAXN];

bool find(int x)
{
    visx[x] = 1;
    for(int i = 1; i < yn; i++)
    {
        if(!visy[i] && mat[x][i] != 0)
        {
            int temp = lx[x] + ly[i] - mat[x][i];

            if(temp == 0)
            {
                visy[i] = 1;
                if(!link[i] || find(link[i]))
                {
                    link[i] = x;
                    return 1;
                }
            }
            else
            {
                slack[i] = min(slack[i], temp);
            }
        }
    }
    return 0;
}

void km()
{
    memset(ly, 0, sizeof(ly));

    for(int i = 1; i < xn; i++)
    {
        lx[i] = -INF;
        for(int j = 1; j < yn; j++)
        {
            lx[i] = max(lx[i], mat[i][j]);
        }
    }

    for(int i = 1; i < xn; i++)
    {
        memset(slack, 127, sizeof(slack));
        while(1)
        {
            memset(visy, 0, sizeof(visy));
            memset(visx, 0, sizeof(visx));

            if(find(i)) break;

            int d = INF;
            for(int j = 1; j < yn; j++)
            if(!visy[j]) d = min(d, slack[j]);

            for(int j = 1; j < xn; j++)
            {
                if(visx[j]) lx[j] -= d;
            }

            for(int j = 1; j < yn; j++)
            {
                if(visy[j]) ly[j] += d;
                else
                {
                    slack[j] -= d;
                }
            }
        }
    }

}

int main()
{
    //freopen("C:/Users/zts/Desktop/in.txt", "r", stdin);
    while(cin >> n >> m && n)
    {
        xn = yn = 1;
        memset(link, 0, sizeof(link));
        memset(mat, -127, sizeof(mat));

        char temp;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin >> temp;
                if(temp == 'm') man[xn].row = i, man[xn].col = j, xn++;
                else if(temp == 'H') house[yn].row = i, house[yn].col = j, yn++;
            }
        }

        for(int i = 1; i < xn; i++)
        {
            for(int j = 1; j < yn; j++)
            {
                mat[i][j] = -(abs(man[i].row - house[j].row) + abs(man[i].col - house[j].col));
            }
        }

        km();

        int ans = 0;

        for(int i = 1; i < yn; i++)
        {
            if(link[i] != 0)
            {
                ans += mat[link[i]][i];
            }
        }

        cout << -ans << endl;
    }

        return 0;
}




内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值