HDU [P1533]
二分图带权最小匹配(朴素)
只要换几个不等号的方向就行,不需要变换权值的正负
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
int n,m,wei[105][105],lx[105],ly[105],match[105];
bool fx[105],fy[105];
struct point{
int x,y;
}men[105],hou[105];
bool hungarian(int u){
fx[u]=1;
for(int v=1;v<=m;v++){
if(!fy[v]&&(lx[u]+ly[v]==wei[u][v])){
fy[v]=1;
if(!match[v]||hungarian(match[v])){
match[v]=u;
return 1;
}
}
}
return 0;
}
int main(){
while(1){
int row,col;
cin>>row>>col;
if(!row&&!col) break;
n=m=0;
for(int i=1;i<=row;i++){
for(int j=1;j<=col;j++){
char t;
scanf(" %c ",&t);
if(t=='m') men[++n].x=i,men[n].y=j;
if(t=='H') hou[++m].x=i,hou[m].y=j;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
// printf("%d %d %d %d\n",men[i].x,men[i].y,hou[j].x,hou[j].y);
wei[i][j]=abs(men[i].x-hou[j].x)+abs(men[i].y-hou[j].y);
}
}
memset(lx,0x3f,sizeof(lx)); //
memset(ly,0,sizeof(ly));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
lx[i]=min(lx[i],wei[i][j]);
}
}
memset(match,0,sizeof(match));
for(int i=1;i<=n;i++){
while(1){
memset(fx,0,sizeof(fx));
memset(fy,0,sizeof(fy));
if(hungarian(i)) break;
int mi=-0x3f3f3f3f; //
for(int k=1;k<=n;k++){
if(fx[k]){
for(int j=1;j<=m;j++){
if(!fy[j]&&(lx[k]+ly[j]-wei[k][j])>mi){ //
mi=lx[k]+ly[j]-wei[k][j];
}
}
}
}
for(int i=1;i<=n;i++){
if(fx[i]) lx[i]-=mi;
}
for(int i=1;i<=m;i++){
if(fy[i]) ly[i]+=mi;
}
}
}
int ans=0;
for(int i=1;i<=m;i++){
if(match[i]) ans+=wei[match[i]][i];
//cout<<ans<<endl;
}
cout<<ans<<endl;
}
}
HDU [P1533]的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- 2017西安网络赛 F
f(cos(x))=cos(n∗x) holds for all xx. Given two integers nn and mm, you need to calculate the coeffic ...
- div排版+文档流+定位秘诀
由于没有找到自己认为完整的关于普通流.浮动和绝对定位的中文文章,于是鼓起勇气决定自己来写篇. 在普通流中的 Box(框) 属于一种 formatting context(格式化上下文) ,类型可以是 ...
- Spring框架学习笔记(8)——AspectJ实现AOP
使用代理对象实现AOP虽然可以满足需求,但是较为复杂,而Spring提供一种简单的实现AOP的方法AspectJ 同样的计算器的DEMO 首先配置applicationContext.xml < ...
- [国嵌攻略][052][NandFlash驱动设计_读]
NandFlash读数据方式 1.页读,读出页中主数据区的所有数据,提供页地址(行地址) 2.随机读,读出页中指定的存储单元的数据,提供页地址(行地址)和页内偏移(行地址) 代码编写 1.根据Nand ...
- java.lang.NoSuchMethodError: javax.wsdl.xml.WSDLReader.readWSDL(Ljavax/wsdl/xml/WSDLLocator;Lorg/w3c/dom/Element;)Ljavax/wsdl/Definition;
http://stackoverflow.com/questions/6066054/whats-wrong-with-my-apache-cxf-client You likely have a 1 ...
- in运算符(javascript)
in的用法,如x in y: 1.如果第二个运算数为对象,则in运算符用来检测第一个运算数是否是第二个运算数的属性名.是,返回true,否则返回false. 例: var obj = {x:1,y:2 ...
- Oracle_建表
Oracle_建表 --设计要求: --建立一张用来存储学生信息的表 --字段包含学号.姓名.性别,年龄.入学日期.班级,email等信息 create table student( ...
- EntityFramework默认映射规则
我不太习惯通过CodeFirst去维护数据库(尽管这是未来实现自动编程的必经之路),还是喜欢通过数据库设计工具如PowerDesigner去建表.如果不想对EF的实体和数据表做什么映射的话,就要注意默 ...
- 虚拟主机,VPS,云主机之间的区别?
虚拟主机即共享主机,是利用虚拟技术把一台完整的服务器分成若干个主机,拥有多个网站,共享这台服务器的硬件和带宽的资源.可以托管简单的静态和动态的网站,满足客户最基本的网络托管需求. VPS是将一台物理服 ...
- Core Graphics框架是Quartz的核心,也是内容描画的基本接口。
Core Graphics框架是Quartz的核心,也是内容描画的基本接口.