题意:给出一个地图,地图上有人和房子,问如何分配哪个人去哪个房子,走的路最短?

这道题是个完备匹配的情况下,问怎么才能走的路最少,可以用KM来做。

只不过KM算法是用来求解最大最优值,所以我们得改一下数据,将每个人去房子的路程都改为负数。

最后再得出 -KM()即刻

 #include<cstdio>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
const int maxn=1e2+;
const int inf=0x3f3f3f3f;
int lx[maxn],ly[maxn];
int match[maxn];
int visx[maxn],visy[maxn];
struct node
{
int x,y;
}a[maxn],b[maxn];
int G[maxn][maxn];
int numa,numb;
int dfs(int k)
{
visx[k]=;
for(int i=;i<=numa;i++){
if(!visy[i]&&G[k][i]==lx[k]+ly[i]){
visy[i]=;
if(!match[i]||dfs(match[i])){
match[i]=k;
return ;
}
}
}
return ;
}
int KM()
{
for(int i=;i<=numa;i++){
lx[i]=-inf,ly[i]=;
for(int j=;j<=numa;j++)
lx[i]=max(lx[i],G[i][j]);
}
for(int k=;k<=numa;k++){
while(){
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(dfs(k)) break;
int mn=inf; for(int i=;i<=numa;i++) if(visx[i])
for(int j=;j<=numa;j++) if(!visy[j])
mn=min(mn,lx[i]+ly[j]-G[i][j]);
if(mn==inf) return -;
for(int i=;i<=numa;i++) if(visx[i]) lx[i]-=mn;
for(int i=;i<=numa;i++) if(visy[i]) ly[i]+=mn;
}
}
int ans=;
for(int i=;i<=numa;i++)
if(match[i])
ans+=G[match[i]][i];
return ans;
}
void init()
{
numa=numb=;
memset(match,,sizeof(match));
memset(G,,sizeof(G));
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
if(n==&&m==) break;
init();
char t;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>t;
if(t=='H'){
numa++;
a[numa].x=i;
a[numa].y=j;
}
else if(t=='m'){
numb++;
b[numb].x=i;
b[numb].y=j;
}
}
}
for(int i=;i<=numa;i++){
int x1=a[i].x,y1=a[i].y;
for(int j=;j<=numb;j++){
int x2=b[j].x,y2=b[j].y;
int w=fabs(x1-x2)+fabs(y1-y2);
G[i][j]=-w;
}
}
printf("%d\n",-KM());
}
return ;
}

KM poj 2195的更多相关文章

  1. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  2. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  3. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  4. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

  5. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  6. poj 2195 Going Home(最小费最大流)

    poj 2195 Going Home Description On a grid map there are n little men and n houses. In each unit time ...

  7. poj 2195 KM算法

    题目链接:http://poj.org/problem?id=2195 KM算法模板~ 代码如下: #include "stdio.h" #include "string ...

  8. POJ 2195 Going Home(KM算法模板)

    题目链接:http://poj.org/problem?id=2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致. man每移动一格需花费$1 ...

  9. poj 2195(KM求最小权匹配)

    题目链接:http://poj.org/problem?id=2195 思路:我们都知道KM使用来求最大权匹配的,但如果要求最小权匹配,只需把图中的权值改为负值,求一次KM,然后权值和取反即可. ht ...

随机推荐

  1. Leetcode Week1 Regular Expression Matching

    Question Given an input string (s) and a pattern (p), implement regular expression matching with sup ...

  2. redis安装并设置开机启动

    1.下载并上传redis安装包至linux服务器目录:/usr/local/redis. 2.解压:tar -zxvf redis-5.0.7.tar.gz 3.编译安装:make && ...

  3. ActiveMQ使用JDBC持久化

    步骤一:创建一个数据库            步骤二:配置activemq.xml配置文件                1.在persistenceAdapter加入如下配置 <!--crea ...

  4. Unable to create initial connections of pool. spring boot mysql

    Unable to create initial connections of pool. 在链接url里添加 将useSSL=true改为useSSL=false 只能说明服务器没有打开SSL功能

  5. npm 升级到最新版本

    先npm -v查看自己的npm 是否是最新版本,如果不是则进入安装node的文件夹,可通过 where node 查找该文件夹. 进入之后使用: npm i npm -g 之后使用: npm -v 查 ...

  6. C++——指针4

    8.对象指针  声明: 类名 *对象指针名 Point A(5,10): Point *ptr; ptr=&A;//通过指针访问对象成员:对象指针名->成员名.ptr->getX( ...

  7. navicat连接mysql8.0+版本报错2059

    ERROR 2059 : Authentication plugin 'caching_sha2_password' cannot be loaded 问题: 连接Docker启动的mysql出现:E ...

  8. java 学习(day1)

    之前学java没好好听课,会一点又不熟练,于是准备重新开始学一些细节,记录每日所学新知识. a+b java的a+b很有意思,当你输出的是" "+a+b,先假设a=2,b=3.然后 ...

  9. JS对象简介

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. sql语言分类及区别、显示和隐示提交的指令

    SQL的发展是从1974年开始的,其发展过程如下: 1974年-----由Boyce和Chamberlin提出,当时称SEQUEL. 1976年-----IBM公司的Sanjase研究所在研制RDBM ...