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

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

#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. HDU1465-装错信封-递推

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. 电脑打不开网页,使用dns优化下就可以了。

    通过电脑管家dns优化下就可以了.启用114DNS

  3. Android初学:Gradle 'HelloWorld' project refresh failed

    Gradle 'HelloWorld' project refresh failed Error:Failed to open zip file.Gradle's dependency cache m ...

  4. light oj 1184 Marriage Media

    题目: You run a marriage media. You take some profiles for men and women, and your task is to arrange ...

  5. Oracle:FOR循环语句练习

    --打印输出从1到10的正整数DECLARE v_i NUMBER(10) := 0;BEGIN LOOP v_i := v_i + 1; DBMS_OUTPUT.put_line(v_i); EXI ...

  6. 【编程技巧】JAVA读取url地址中的文本内容

    应用场景:最常见的是有自已的网站,在注册广告联盟的时候.都需要下载一个文本文件来验证网站的有效性.例如淘宝.京东等都有这一过程 实现代码://读url地址的内容        public void ...

  7. SQL Constraints

    每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束. http://www.w3school.com.cn/sql/sql_unique.asp 另外相关:@On ...

  8. angular 4 http 之web api 服务

    Angular Http是获取和保存数据的.主要是为了取到我json文件里的数据. 直接上代码吧: 1.  先介绍Promise模式的:(直接代码) heroes.json: 1 2 3 4 5 6 ...

  9. SpringMVC运行原理

    一.SpringMVC运行原理图 ​ 二.相关接口解释 DispatcherServlet接口: Spring提供的前端控制器,所有的请求都有经过它来统一分发.在DispatcherServlet将请 ...

  10. 利用overflow实现导航栏中常 出现的倒三角下拉小图标

    常用网页界面中,导航栏中的倒三角下拉小图标实现,可用overflow: 效果如右图: .Triangle{position:relative;width:280px;height:15px;} ;ov ...