用二分图模型解决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排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

关于跨域无效的问题及解决(java后端方案)

《关于跨域无效的问题及解决(java后端方案)》:本文主要介绍关于跨域无效的问题及解决(java后端方案),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录通用后端跨域方法1、@CrossOrigin 注解2、springboot2.0 实现WebMvcConfig

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码

解决JSONField、JsonProperty不生效的问题

《解决JSONField、JsonProperty不生效的问题》:本文主要介绍解决JSONField、JsonProperty不生效的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录jsONField、JsonProperty不生效javascript问题排查总结JSONField

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Swagger在java中的运用及常见问题解决

《Swagger在java中的运用及常见问题解决》Swagger插件是一款深受Java开发者喜爱的工具,它在前后端分离的开发模式下发挥着重要作用,:本文主要介绍Swagger在java中的运用及常... 目录前言1. Swagger 的主要功能1.1 交互式 API 文档1.2 客户端 SDK 生成1.3

java连接opcua的常见问题及解决方法

《java连接opcua的常见问题及解决方法》本文将使用EclipseMilo作为示例库,演示如何在Java中使用匿名、用户名密码以及证书加密三种方式连接到OPCUA服务器,若需要使用其他SDK,原理... 目录一、前言二、准备工作三、匿名方式连接3.1 匿名方式简介3.2 示例代码四、用户名密码方式连接4

如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socket read timed out的问题

《如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socketreadtimedout的问题》:本文主要介绍解决Druid线程... 目录异常信息触发场景找到版本发布更新的说明从版本更新信息可以看到该默认逻辑已经去除总结异常信息触发场景复