HDU--1533--Going Home--KM算法
Going Home
step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates
there is a little man on that point.

You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
2
10
28
#include <iostream>
#include <cstring>
#define MAX (1<<30)
#define MIN -MAX
using namespace std;
struct ssss
{
int x,y;
};
ssss s1[111],s2[111];
int n,m,l1,l2,map[111][111];
int rode[111],r[111];
bool vx[111],vy[111];
int sx[111],sy[111];
int dfs(int x)
{
vx[x]=1; //标记增广路左边已訪问的点
int i,j,k,l;
for(i=0;i<l2;i++)
if(!vy[i])
{
k=sx[x]+sy[i]-map[x][i];
if(k==0)
{
vy[i]=1;//訪问它再标记已訪问
if(rode[i]==-1||dfs(rode[i])) //假设右边的点没有匹配或者有匹配(继续用他的匹配点继续找)
{
rode[i]=x; //记录右边点匹配到的左边点的序号
return 1;
}
}else if(r[i]>k)r[i]=k; //记录右端点没訪问的边的最小差值。用来导入
}
return 0;
}
int Dinic()
{
int i,j,k,l;
memset(sy,0,sizeof(sy)); //标记右端点权值
memset(rode,-1,sizeof(rode)); //右端点匹配点初始化为-1
for(i=0;i<l1;i++)
{
sx[i]=MIN;
for(j=0;j<l2;j++)
sx[i]=max(sx[i],map[i][j]); //左端点权值取最大的边的值
}
for(i=0;i<l1;i++)
{
for(j=0;j<l2;j++)r[j]=MAX;
while(1)
{
memset(vx,0,sizeof(vx)); //訪问标记初始化
memset(vy,0,sizeof(vy));
if(dfs(i))break; //匹配到了就结束
k=MAX;
for(j=0;j<l2;j++)
if(!vy[j])k=min(k,r[j]); //不然导入差值最小的边(这是保证匹配的总值从最大逐渐减小)
for(j=0;j<l1;j++)
if(vx[j])sx[j]-=k; //左端点权值减小
for(j=0;j<l2;j++)
if(vy[j])sy[j]+=k; //右端点权值曾加
//这样导入了边之后其它匹配不变x+y=(x-k)+(y+k)
}
}
for(i=k=0;i<l2;i++)
k+=map[rode[i]][i];
return -k;
}
int bb(int x)
{
return x>0?x:-x;
}
int main (void)
{
int i,j,k,l;
char c;
while(cin>>n>>m&&n)
{
l1=l2=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
cin>>c;
if(c=='m')
{
s1[l1].x=i;
s1[l1].y=j;
l1++;
}
if(c=='H')
{
s2[l2].x=i;
s2[l2].y=j;
l2++;
}
}
for(i=0;i<l1;i++)
for(j=0;j<l2;j++)
{
k=bb(s1[i].x-s2[j].x)+bb(s1[i].y-s2[j].y);
map[i][j]=(k<0?k:-k);
}
cout<<Dinic()<<endl;
}
return 0;
}
HDU--1533--Going Home--KM算法的更多相关文章
- hdu 1533 Going Home (KM)
Going HomeTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 2255 奔小康赚大钱--KM算法模板
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 题意:有N个人跟N个房子,每个人跟房子都有一定的距离,现在要让这N个人全部回到N个房子里面去,要 ...
- hdu 2853 Assignment KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Last year a terrible earthquake attacked Sichuan ...
- hdu 2426 Interesting Housing Problem 最大权匹配KM算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2426 For any school, it is hard to find a feasible ac ...
- 【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, ...
- 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 2255 奔小康赚大钱 (KM算法 模板题)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 3488(KM算法||最小费用最大流)
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- HDU:2255-奔小康赚大钱(KM算法模板)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Mem ...
随机推荐
- cocos2d-x for js 继承的写法
cocos2d-x for js中集成了两套继承写法,一套是JR的(jquery的作者),一套是google. 目前来说,cocos2d-x for js demo使用JR的写法----未完待续... ...
- use-a, has-a, is-a和实现关系
use-a关系 如果类A与类B是 use-a 关系,则A具有类型为B.B&.const B&.B*.const B*的一个成员,或者是可以轻易以上述对象之一 返回一个B的函数.于是A可 ...
- ODBC具体使用
应用程序 应用程序对外提供使用者交谈界面,同时对内执行资料之准备工作数据库系统所传回来的结果在显示给使用者看.简单来说,应用程序即ODBC 界面执行下列主要工作:1. Request a connec ...
- 【HDOJ】1114 Piggy-Bank
DP,先将coins按照重量排序可以优化. #include <stdio.h> #include <stdlib.h> #define MAXNUM 10005 #defin ...
- WCF - Developers Tools
For developing a WCF service application, there are mainly two tools – Microsoft Visual Studio and C ...
- php include include_once require require_once 的区别与联系
一.require 与 include 的区别: The require() function is identical to include(), except that it handles er ...
- (转载)SQL Server 2005 日志文件过大处理
由于安装的时候没有计划好空间,默认装在系统盘,而且又没有做自动备份.截断事务日志等,很快LDF文件就达到十几G,或者几十G ,此时就不得不处理了. 备份和计划就不说了,现在就说下怎么把它先删除吧: 1 ...
- BrnShop开源网上商城第五讲:自定义视图引擎
今天这篇博文主要讲解自定义视图引擎,大家都知道在asp.net mvc框架中默认自带一个Razor视图引擎,除此之外我们也可以自定义自己的视图引擎,只需要实现IViewEngine接口,接口定义如下: ...
- SVN使用教程之——分支、合并
首先说下为什么我们需要用到分支-合并.比如项目demo下有两个小组,svn下有一个trunk版.由于客户需求突然变化,导致项目需要做较大改动,此时项目组决定由小组1继续完成原来正进行到一半的工作[某个 ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...