用二分图模型解决poj 2195

2024-05-27 13:58
文章标签 模型 二分 解决 poj 2195

本文主要是介绍用二分图模型解决poj 2195,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       之前这道题用了费用流来解决,这次用了二分图最佳匹配来解决。

       不过,用二分图最佳匹配解决这道题时,要注意题目要求花费最小,所以是求权值之和最小的最佳匹配。要用一个大数减去原有权值作为新的权值,最后输出答案时,要注意还原真正的权值。

       通过这道题,也可以发现费用流与二分图最佳匹配之间有所关联,而且就这道题来看,二分图的代码量会小于费用流的代码量,所以尽可能采用二分图模型来解决问题可能会简单些。

代码(C++):

#include <cstdlib>
#include <iostream>
#include <cmath>#define MAX 150
#define INF (1<<30)
using namespace std;//#define LOCALtypedef pair<int,int> pii;
pii array_h[MAX],array_m[MAX];int w[MAX][MAX],lx[MAX],ly[MAX],cy[MAX],vx[MAX],vy[MAX];bool match(int u,int n)
{int i;vx[u]=true;for(i=0;i<n;i++){if(lx[u]+ly[i]==w[u][i]&&!vy[i]){vy[i]=true;                            if(cy[i]==-1||match(cy[i],n)){cy[i]=u;return true;                       }                            }            } return false;
}void update(int n)
{int i,j,d;d=INF;for(i=0;i<n;i++) if(vx[i]){for(j=0;j<n;j++) if(!vy[j]){d=min(d,lx[i]+ly[j]-w[i][j]);                     }             } for(i=0;i<n;i++){if(vx[i]) lx[i]-=d;if(vy[i]) ly[i]+=d;            }
}int km(int n)
{int i,j,ans;for(i=0;i<n;i++){lx[i]=-INF;ly[i]=0;cy[i]=-1;for(j=0;j<n;j++) lx[i]=max(lx[i],w[i][j]);           }for(i=0;i<n;i++){while(1){memset(vx,false,sizeof(vx));memset(vy,false,sizeof(vy));if(match(i,n)) break;else update(n);    }            }ans=0;for(i=0;i<n;i++) ans+=w[cy[i]][i];return ans;
}int main(int argc, char *argv[])
{
#ifdef LOCAL freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endifint n,m,a,b,i,j,fee,max;char ch;while(scanf("%d %d",&n,&m)&&n+m!=0){a=b=0;            for(i=0;i<n;i++){getchar();            for(j=0;j<m;j++){scanf("%c",&ch);if(ch=='H') array_h[a++]=make_pair(i,j);else if(ch=='m') array_m[b++]=make_pair(i,j);        }            }max=n+m;for(i=0;i<b;i++){for(j=0;j<a;j++){fee=abs(array_m[i].first-array_h[j].first)+abs(array_m[i].second-array_h[j].second);     w[i][j]=max-fee;       }            } printf("%d\n",max*a-km(a));           }system("PAUSE");return EXIT_SUCCESS;
}

题目( http://poj.org/problem?id=2195):

Going Home
Time Limit: 1000MS Memory Limit: 65536K
   

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

这篇关于用二分图模型解决poj 2195的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1007672

相关文章

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

解决RocketMQ的幂等性问题

《解决RocketMQ的幂等性问题》重复消费因调用链路长、消息发送超时或消费者故障导致,通过生产者消息查询、Redis缓存及消费者唯一主键可以确保幂等性,避免重复处理,本文主要介绍了解决RocketM... 目录造成重复消费的原因解决方法生产者端消费者端代码实现造成重复消费的原因当系统的调用链路比较长的时

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

SQL Server安装时候没有中文选项的解决方法

《SQLServer安装时候没有中文选项的解决方法》用户安装SQLServer时界面全英文,无中文选项,通过修改安装设置中的国家或地区为中文中国,重启安装程序后界面恢复中文,解决了问题,对SQLSe... 你是不是在安装SQL Server时候发现安装界面和别人不同,并且无论如何都没有中文选项?这个问题也

java内存泄漏排查过程及解决

《java内存泄漏排查过程及解决》公司某服务内存持续增长,疑似内存泄漏,未触发OOM,排查方法包括检查JVM配置、分析GC执行状态、导出堆内存快照并用IDEAProfiler工具定位大对象及代码... 目录内存泄漏内存问题排查1.查看JVM内存配置2.分析gc是否正常执行3.导出 dump 各种工具分析4.

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

SpringBoot整合Dubbo+ZK注册失败的坑及解决

《SpringBoot整合Dubbo+ZK注册失败的坑及解决》使用Dubbo框架时,需在公共pom添加依赖,启动类加@EnableDubbo,实现类用@DubboService替代@Service,配... 目录1.先看下公共的pom(maven创建的pom工程)2.启动类上加@EnableDubbo3.实