二分图带权最小匹配(朴素)

只要换几个不等号的方向就行,不需要变换权值的正负

#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]的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. 整数n的全排列

    第一道用搜索码的.得纪念一下 #include <iostream> #include <cstdio> #include <cstring> #include & ...

  2. [国嵌攻略][099][Linux内核配置与编译]

    为什么要配置内核 基于硬件和软件的需求选出需要的功能,去掉不要的功能. 内核配置的方法 make config:基于文本交互的配置. make menuconfig:基于图形菜单的配置. make m ...

  3. 第三方推送 JPush 配置中的引入so库问题

    Gradle入门:http://www.infoq.com/cn/articles/android-in-depth-gradle/ 当所需要的so库已经复制到libs目录下,系统还是提示 找不到so ...

  4. 空数组在以下三种遍历中均不可更改:forEach、map和for...in

    首先,我们要知道对于forEach.map和for...in三种遍历,在不是空数组的情况下,要想实现更改原数组的方法,代码如下: var list = [1,2,3,4]; var list1 = [ ...

  5. JAVA:成员变量和局部变量的区别

    1.作用于不同: 局部变量的作用域仅限于定义它的方法 成员变量的作用域在整个类的内部都是可见的 2.初始值不同 JAVA会给成员变量一个初始值 JAVA不会给局部变量赋予初始值 3.在同一个方法中,不 ...

  6. TP5使用phpmailer实现邮件发送

    1.从github下载PHPMailer,在vendor目录中新建文件夹phpmailer,将压缩包中的class.phpmailer.php和class.smtp.php复制到phpmailer中, ...

  7. tp系统常量定义

    (2013-03-06 14:16:31) 转载▼ 标签: it 是已经封装好的系统常量 主要是用在控制器下面的动作当中 这样能很大的提高我们的开发效率 主要有下面的一些      手册上面都有的   ...

  8. 02 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之Service层

    作者:nnngu 项目源代码:https://github.com/nnngu/nguSeckill 首先在编写Service层代码前,我们应该首先要知道这一层到底是干什么的. Service层主要负 ...

  9. 再叙Java反射

    Java中的反射 本文为反射的基础知识部分. 能够分析类能力的程序被称为反射(reflective). 反射机制允许程序在运行时取得任何一个已知名称的class的内部信息,容许程序在运行时加载.探知. ...

  10. CCF系列之画图(201409-2)

    试题编号: 201409-2试题名称: 画图时间限制: 1.0s内存限制: 256.0MB问题描述: 问题描述 在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩形指将横坐标范围从 ...