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

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

#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. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)

    以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...

  2. BZOJ1294: [SCOI2009]围豆豆Bean

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1294 状压dp,dis[s][i][j]表示从(i,j)出发围的状态是s的最短路. 然后判断一 ...

  3. hdu_4497GCD and LCM(合数分解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 GCD and LCM Time Limit: 2000/1000 MS (Java/Other ...

  4. Ubuntu搭建Gitlab服务器

    想到Gitlab就必定会想到SVN,因为两者都是代码管理系统,作为开发人员来说,用习惯了SVN的图形化界面和SVN代码更新和提交的方式, 可能就会觉得使用git会比较麻烦,其实不然git使用起来非常方 ...

  5. Qt 共享库(动态链接库)和静态链接库的创建及调用

    前言: 编译器 Qt Creator, 系统环境 win7 64 位 1.创建共享库: 新建文件或项目->选择 Library 和 c++ 库->选择共享库->下一步(工程名为 sh ...

  6. IntelliJ IDEA 配置 smartGit

    教你如何在IntelliJ IDEA中配置smartGit? 一.第一种方式: 1.在启动IDEA工具时,点击下拉按钮"Check out from Version Control" ...

  7. MLlib--保序回归

    转载请标明出处http://www.cnblogs.com/haozhengfei/p/24cb3f38b55e5d7516d8059f9f105eb6.html 保序回归 1.线性回归VS保序回归 ...

  8. 从零开始学习前端开发 — 3、CSS盒模型

    ★  css盒模型是css的基石,每个html标签都可以看作是一个盒模型. css盒模型是由内容(content),补白或填充(padding),边框(border),外边距(margin)四部分组成 ...

  9. 跟版网 > 织梦教程 > 织梦安装使用 > 织梦DedeCMS附件上传大

    织梦DedeCMS附件上传大小受限制,超过2M就不能上传了,针对此问题按如下方法修改: 1.进入后台→系统设置→系统基本参数→会员设置→会员上传文件大小(K),改成你需要限制的大小: 2.在dede ...

  10. TF-卷积函数 tf.nn.conv2d 介绍

    转自 http://www.cnblogs.com/welhzh/p/6607581.html 下面是这位博主自己的翻译加上测试心得 tf.nn.conv2d是TensorFlow里面实现卷积的函数, ...