题意

  求二分图最小完备匹配。

SOL

  建个图那么方便的事情是吧。。。然后边权都是正的(好像根边权也没什么关系),既然要求最小那么把边权取个相反数跑个KM就好了。。

CODE:

/*==========================================================================
# Last modified: 2016-02-16 19:55
# Filename: hdu1533.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
CODE:
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define lowbit(x) (x)&(-x)
#define INF 1070000000
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define maxn 200
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
/*==================split line==================*/ int n,sumx,sumy;
int w[maxn][maxn],link[maxn],lx[maxn],ly[maxn],slack[maxn],x[maxn],y[maxn];
bool S[maxn],T[maxn];
int calc(int i,int j){
int y1=x[i]%1000,x1=x[i]/1000,y2=y[j]%1000,x2=y[j]/1000;
return -(abs(y1-y2)+abs(x1-x2));
}
void build_graph(){
FORP(i,1,n)
FORP(j,1,n)
w[i][j]=calc(i,j);
}
bool match(int i){
S[i]=true;
FORP(j,1,n){
if (T[j]) continue;
int tmp=lx[i]+ly[j]-w[i][j];
if (tmp==0){
T[j]=true;
if (!link[j] || match(link[j])){
link[j]=i;
return true;
}
}
else slack[j]=min(slack[j],tmp);
}
return false;
}
void updata(){
int a=INF;
FORP(i,1,n) if (!T[i]) a=min(a,slack[i]);
FORP(i,1,n) {
if (S[i]) lx[i]-=a;
if (T[i]) ly[i]+=a;
else slack[i]-=a;
}
}
int KM(){ memset(link,0,sizeof(link));
memset(ly,0,sizeof(ly));
FORP(i,1,n)
FORP(j,1,n) lx[i]=max(lx[i],w[i][j]); FORP(i,1,n){
memset(slack,0x7f,sizeof(slack));
while (1){
memset(S,false,sizeof(S));
memset(T,false,sizeof(T));
if (match(i)) break;
else updata();
}
}
int ans=0;
FORP(i,1,n) if (link[i])
ans+=w[link[i]][i];
return -ans;
}
int main(){
//freopen("a.in","r",stdin);
int r,c;
while (1){
sumx=0; sumy=0;
scanf("%d%d",&r,&c);
if (r==0 && c==0) return 0;
FORP(i,1,r){
char ch[1000];
scanf("%s",ch);
FORP(j,0,c-1)
if (ch[j]=='m') x[++sumx]=i*1000+j+1;
else if (ch[j]=='H') y[++sumy]=i*1000+j+1;
}
n=sumx;
build_graph();
printf("%d\n",KM());
}
}

HDU 1533 & KM模板的更多相关文章

  1. HDU 2255 & KM模板

    题意: 一张完备二分图求最优完备匹配. SOL: 这题就不讲什么sol了...毕竟是裸的KM,不会的话可以看老人家的大白鼠,一些问题看代码注释.讲讲经历(悲惨的经历) 刚打完,自信地交上去发现MLE. ...

  2. HDU 1533 KM算法(权值最小的最佳匹配)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. hdu 1533 KM或费用流

    以前用KM写过,现在再用费用流写. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  4. 【HDU 1533】 Going Home (KM)

    Going Home Problem Description On a grid map there are n little men and n houses. In each unit time, ...

  5. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  6. HDU 1533 Going Home(KM完美匹配)

    HDU 1533 Going Home 题目链接 题意:就是一个H要相应一个m,使得总曼哈顿距离最小 思路:KM完美匹配,因为是要最小.所以边权建负数来处理就可以 代码: #include <c ...

  7. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  8. HDU 2138 Miller-Rabin 模板题

    求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...

  9. HDU 1533 最小费用最大流(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...

随机推荐

  1. Ubuntu 下 LAMP 的配置文件路径 转:

      配置文件路径: 1>apache 的配置文件路径 /etc/apache2/apache2.conf 2>apache 网站字符编码配置路径 /etc/apache2/conf.d/c ...

  2. linux 系统下查看raid信息,以及磁盘信息

    有时想知道服务器上有几块磁盘,如果没有做raid,则可以简单使用fdisk -l  就可以看到. 但是做了raid呢,这样就看不出来了.那么如何查看服务器上做了raid? 软件raid:只能通过Lin ...

  3. 使用ifconfig命令给网卡配置ip别名

    给网卡eth0配置一个ip别名 sudo ifconfig eth0:0 10.108.125.6/22 up 若想保存该配置,以便每次开机都可以使用该ip别名,则应 sudo vim /etc/ne ...

  4. intellij idea 如何更改编辑器文本字体和大小

    换上了intellij idea之后,第一件事就是想要改变下文字字体,因为在我这个27寸的2k分辨率的屏幕上,文字显然太小了. intellij idea字体设值分成两部分,一部分是UI部分字体字号设 ...

  5. 【SpringMVC】SpringMVC系列15之SpringMVC最佳实践

    15.SpringMVC最佳实践 15.1.遵循Restful API最佳实践 参考:http://segmentfault.com/a/1190000002949234 15.2.统一返回字段 15 ...

  6. OAuth

    http://oauth.net http://oauth.net/2/ http://tools.ietf.org/html/rfc6749 人人网:http://wiki.dev.renren.c ...

  7. 37.寻找丑数[Ugly numbers]

    [题目] 我们把只包含质因子2.3和5的数称作丑数(Ugly Number),例如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第1500个丑 ...

  8. 用RPM包安装MySQL的默认安装路径问题

    在安装PHP时候要对一些配置选项进行设置,其中就有:--with-mysql[=DIR]:包含MySQL扩展,[=DIR]指定mysql安装目录,省略[=DIR]则为默认位置/usr--with-my ...

  9. Java for LeetCode 042 Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  10. java关闭流,解压缩后的清除

    关闭流文件和file文件的时候,先打开的后关闭,后打开的先关闭,实在不行调用system.jc()方法