【HDU 1533】 Going Home (KM)
Going Home
Problem DescriptionOn a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every 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.InputThere are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the 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.OutputFor each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.Sample Input2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0Sample Output2
10
28Source
给你一个类似这样的图
...H....
...H....
...H....
mmmHmmmm
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 110
#define Maxm 10010
#define INF 0xfffffff struct node
{
int x,y,c,next;
}t[Maxm];int len;
int first[Maxn]; int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;}
int myabs(int x) {return x>?x:-x;} int hx[Maxn],hy[Maxn],mx[Maxn],my[Maxn];
int fn; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=-c;
t[len].next=first[x];first[x]=len;
} int lx[Maxn],ly[Maxn];
bool visx[Maxn],visy[Maxn];
int slack[Maxn],match[Maxn]; char s[Maxn]; bool ffind(int x)
{
visx[x]=;
for(int i=first[x];i;i=t[i].next) if(!visy[t[i].y])
{
int y=t[i].y;
if(lx[x]+ly[y]==t[i].c)
{
visy[y]=;
if(!match[y]||ffind(match[y]))
{
match[y]=x;
return ;
}
}
else slack[y]=mymin(slack[y],lx[x]+ly[y]-t[i].c);
}
return ;
} void solve()
{
memset(match,,sizeof(match));
memset(ly,,sizeof(ly));
// memset(lx,0,sizeof(lx));
for(int i=;i<=fn;i++)
{
lx[i]=-INF;
for(int j=first[i];j;j=t[j].next) lx[i]=mymax(lx[i],t[j].c);
} for(int i=;i<=fn;i++)
{
for(int j=;j<=fn;j++)
slack[j]=INF;
while()
{
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if(ffind(i)) break; int delta=INF;
for(int j=;j<=fn;j++) if(!visy[j])
delta=mymin(delta,slack[j]); if(delta==INF) return ; for(int j=;j<=fn;j++)
{
if(visx[j]) lx[j]-=delta;
if(visy[j]) ly[j]+=delta;
else slack[j]-=delta;
}
}
}
} int main()
{
int n,m;
while()
{
scanf("%d%d",&n,&m);
if(n==&&m==) break;
hx[]=mx[]=;
for(int i=;i<=n;i++)
{
scanf("%s",s);
for(int j=;j<m;j++)
{
if(s[j]=='H') hx[++hx[]]=i,hy[hx[]]=j+;
else if(s[j]=='m') mx[++mx[]]=i,my[mx[]]=j+;
}
}
len=;
memset(first,,sizeof(first));
for(int i=;i<=hx[];i++)
for(int j=;j<=mx[];j++)
ins(i,j,myabs(hx[i]-mx[j])+myabs(hy[i]-my[j]));
fn=hx[];
solve();
int ans=;
for(int i=;i<=fn;i++) ans+=lx[i]+ly[i];
printf("%d\n",-ans);
}
return ;
}
HDU 1533
容易打错的地方是visx 和 visy 的标记。
表示的是是否为增广路上的点,前提当然是他也在相等子图上。
2016-10-27 09:45:40
【HDU 1533】 Going Home (KM)的更多相关文章
- 【HDU 4992】 Primitive Roots (原根)
Primitive Roots Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...
- 【HDU - 2102】A计划(bfs)
-->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...
- 【hdu 5918】Sequence I(KMP)
给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...
- 【HDU 5839】Special Tetrahedron(计算几何)
空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...
- 【HDU 4445】Crazy Tank(暴力)
高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...
- 【HDU 4343】Interval query(倍增)
BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...
- 【HDU 6153】A Secret (KMP)
Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...
- 【HDU 6008】Worried School(模拟)
Problem Description You may already know that how the World Finals slots are distributed in EC sub-r ...
- 【HDU 4763】Theme Section(KMP)
这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...
随机推荐
- Java项目依赖的配置过程
我们经常需要把大项目分解成多个小项目,然后使用依赖方式,将其他必须的依赖项目引用到项目中.最常用的方式是希望被依赖的项目能够自动被打包成jar文件,自动部署到依赖的项目中.同时被依赖项目修改后,能够同 ...
- 在iframe中获取父页面的元素
a.html <!DOCTYPE html> <html> <head> <title></title> </head> < ...
- http://venkatbaggu.com/file-upload-in-asp-net-mvc-using-dropzone-js-and-html5/
http://venkatbaggu.com/file-upload-in-asp-net-mvc-using-dropzone-js-and-html5/ http://www.cnblogs.co ...
- R-大数据分析挖掘(4-R爬虫实现)
library("XML") #获取全部的链接 url <- 'http://www.csdn.net/tag/' i_url_parse<-htmlParse(ur ...
- .net+easyui系列--Pagination 分页
使用 JS 创建分页 <div id="pat" style="background:#efefef;border:1px solid #ccc;"> ...
- 获取select下拉列表选中的值
html: <select id="resultList"> <option >1班</option> <option >2班< ...
- Android adapter适配器的使用
说起Adapter的使用,首先想到的就是listview或各种各样的Adapter.下面我们对常用的一些Adapter进行简单的使用讲解. 这是Adapter的关系图: 下面的所有例子均使用同一个布局 ...
- 学会用Reflector调试我们的MVC框架代码
我们知道,现在能调试.net程序通常有两个,第一个是ILSpy,还是一个是Reflector,这两个小反编译软件算是我们研究底层代码中所拥有的一把 锋利小尖刀~~~,比如你看到的ILSpy这样的界面图 ...
- gdal中文路径无法打开问题
在C#中使用OGR读写矢量数据时,需要引用“using OSGeo.OGR;”. 同时为了处理中文路径和中文字段,需要在开始设置下面两个属性,代码如下: //为了支持中文路径,请添加下面这句代码(大多 ...
- WPF窗体视图中绑定Resources文件中字符串时,抛出:System.Windows.Markup.StaticExtension
问题描述: 在Resources.resx定义了一个静态字符串字段Title,并在WPF窗体视图中绑定为窗体的标题: Title="{x:Static local:Resources.Tit ...