KM poj 2195
题意:给出一个地图,地图上有人和房子,问如何分配哪个人去哪个房子,走的路最短?
这道题是个完备匹配的情况下,问怎么才能走的路最少,可以用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的更多相关文章
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- POJ 2195 Going Home (带权二分图匹配)
POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...
- POJ 2195 Going Home 最小费用最大流 尼玛,心累
D - Going Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- 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 ...
- poj 2195 KM算法
题目链接:http://poj.org/problem?id=2195 KM算法模板~ 代码如下: #include "stdio.h" #include "string ...
- POJ 2195 Going Home(KM算法模板)
题目链接:http://poj.org/problem?id=2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致. man每移动一格需花费$1 ...
- poj 2195(KM求最小权匹配)
题目链接:http://poj.org/problem?id=2195 思路:我们都知道KM使用来求最大权匹配的,但如果要求最小权匹配,只需把图中的权值改为负值,求一次KM,然后权值和取反即可. ht ...
随机推荐
- The Number of Inversions
For a given sequence A={a0,a1,...an−1}A={a0,a1,...an−1}, the number of pairs (i,j)(i,j) where ai> ...
- Cow Contest POJ - 3660 floyd传递闭包
#include<iostream> #include<cstring> using namespace std; ,INF=0x3f3f3f3f; int f[N][N]; ...
- 从0开始学算法--排序(1.12c++利用标准库排序)
1,简单数组按升序排序 sort(a,a+n); #include <algorithm> #include <iostream> #include <cstring&g ...
- 简单的登录验证小程序_python
一.要求 输入用户名密码,验证成功之后显示欢迎信息,输错三次后锁定. 程序: #!/usr/bin/env python# _*_ coding:utf-8 _*_#Author:chenxz #将黑 ...
- AVR单片机丢固件原因分析和解决方案
一.硬件方面 除了下面列举的方面,还需要评估下其他措施. 1.电源因素,禁干扰. 只要用廉价劣质的开关电源,不管哪个单片机,都存在EEPROM丢数据和单片机程序丢失的情况. 1.转接板走线,直接接到了 ...
- sql server和my sql 命令(语句)的区别,sql server与mysql的比较
sql与mysql的比较 1.连接字符串sql :Initial Catalog(database)=x; --数据库名称 Data Source(source)=x; - ...
- Introduction to Writing Functions in R
目录 在R中编写函数 args(函数名) 创建一个函数的步骤 1.default args Passing arguments between functions Checking arguments ...
- 在sql server中如何检测一个字符串中是否包含另一个字符串
select CHARINDEX('456','123456') SQL语句使用CHARINDEX函数,来测试一个字符串中是否包含另一个字符串中的方法: 一.CHARINDEX函数介绍 1.函数功 ...
- java-判断年份是不是闰年
if ((year%4==0)&&(year%100!=0)||(year%400==0)) {//是闰年 leapYear = true; }
- Unity中常用的数据结构总结
本篇博文对U3D经常用到的数据结构和各种数据结构的应用场景总结下. 1.几种常见的数据结构 这里主要总结下在工作中常碰到的几种数据结构:Array,ArrayList,List<T>,Li ...