【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的思 ...
随机推荐
- (转+整理) oracle authid definer 与 authid current_user
转:http://blog.csdn.net/indexman/article/details/17067531 http://blog.csdn.net/liqfyiyi/article/detai ...
- Unity3D 关于运动模型
首先,要让一个物体在3D世界里面运动起来. 要在一个具有碰撞器的面和一个具有碰撞器的体. 要具有碰撞器属性,不然其他物体会穿过的. 默认的碰撞器是一个具有阻碍效果的,如果让碰撞器具有了一个触发器属性I ...
- Mysql 的一些异常解决
一.关于大文件存储 1.利用mysql存储大文件时,异常截图 在配置文件中加上如下一行 2.改完后重启mysql,但是又报如下错误: 解决方案: 我的mysql 是5.6版本,查到网上说要修改配置文件 ...
- hadoop_集群安装_2
由于上一篇文章http://www.cnblogs.com/inuyasha1027/p/hadoop_cluster_install_1.html 截图太多,占用了太多的地方,所以将VMTools ...
- MyEclipse8.5注册码生成
步骤: 1.建立一个任意名称的Java project: 2.在该工程中建立一个名为MyEclipseGen的Java文件(MyEclipseGen.java) 3.运行下边代码,会在控制台出现&qu ...
- 搭建showslow:前端性能跑分及优化工具
综述:showslow是一个开源的工具,集成并通过Yahoo yslow.google page speed.dynaTrace AJAX等工具监测网站各项性能指标,然后通过图表和排名展示出来. 1. ...
- android软件开发之webView.addJavascriptInterface循环渐进【二】
本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...
- java中的JSON对象的使用
申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...
- 2015-01-27-从实验出发理解buffer与cache区别-吴伟顺
通过du(find) 与 cat 体现buffer与cache差异实验: 实验表明: 1 通常 buffer << cache 2 "文件系统"相关内容(ino ...
- rsync 的使用和参数解释
备份往往可以为我们提供一种恢复的策略,因此在实际的生产应用中我们需要对系统的各个配置以及数据进行备份.然而普通的备份都是在本地磁盘或者相应的设备上进行,其实这样也存在一种缺陷,就是设备也出现问题怎么办 ...
