Going Home

Problem Description
On 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.

Input
There 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.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay. 
Sample Input
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
Sample Output
2
10
28
Source
 
【题意】
  

给你一个类似这样的图

...H....

...H....

...H....

mmmHmmmm

...H....
...H....
...H....
问所有H移动到所有m上花费最少的步数
 
【分析】
 
  这题题解都是费用流,可能不卡费用流。
  我打的是n^3 KM,不过求的是最小边权的最佳匹配,所以把边权先取反然后再做。
  记得一开始初始化lx的时候是-INF 不是0,有负边。
 
代码如下:
 #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)的更多相关文章

  1. 【HDU 4992】 Primitive Roots (原根)

    Primitive Roots   Description We say that integer x, 0 < x < n, is a primitive root modulo n i ...

  2. 【HDU - 2102】A计划(bfs)

    -->A计划 Descriptions: 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的 ...

  3. 【hdu 5918】Sequence I(KMP)

    给定两个数字序列,求a序列中每隔p个构成的p+1个序列中共能匹配多少个b序列. 例如1 1 2 2 3 3 每隔1个的序列有两个1 2 3 kmp,匹配时每次主串往前p个,枚举1到p为起点. 题目 # ...

  4. 【HDU 5839】Special Tetrahedron(计算几何)

    空间的200个点,求出至少四边相等,且其余两边必须不相邻的四面体的个数. 用map记录距离点i为d的点有几个,这样来优化暴力的四重循环. 别人的做法是枚举两点的中垂面上的点,再把到中点距离相等的点找出 ...

  5. 【HDU 4445】Crazy Tank(暴力)

    高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...

  6. 【HDU 4343】Interval query(倍增)

    BUPT2017 wintertraining(15) #8D 题意 给你x轴上的N个线段,M次查询,每次问你[l,r]区间里最多有多少个不相交的线段.(0<N, M<=100000) 限 ...

  7. 【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 ...

  8. 【HDU 6008】Worried School(模拟)

    Problem Description You may already know that how the World Finals slots are distributed in EC sub-r ...

  9. 【HDU 4763】Theme Section(KMP)

    这题数据水的一B.直接暴力都能够过. 比赛的时候暴力过的.回头依照正法做了一发. 匹配的时候 失配函数 事实上就是前缀 后缀的匹配长度,之后就是乱搞了. KMP的题可能不会非常直接的出,可是KMP的思 ...

随机推荐

  1. php加载memcache

    安装php加载memcache[root@web-server ~]# rpm -qa | grep libevent [root@web-server ~]# yum -y install libe ...

  2. css字体转换程序(Node.js)

    我下载的是ttf文件,css导入的文件有多种格式:eot,woff,svg 在windows下,需要寻找相应的exe文件来处理或者node.js来处理: ttf2eot: https://github ...

  3. ECMA5 Array 新增API reduce

    1)reduce:相当与迭代: [].reduce(function(previous,current,index,array){ return previous * current;//相当与做阶乘 ...

  4. 向SQL2008R2导入Acess、excel数据

    一:导入Access数据 1.在sql2008查询分析 器中输入如下查询语句能查出access中的数据 SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLE ...

  5. CSS控制长文本内容显示(截取的地方用省略号代替)

    自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,下面介绍的是CSS如何实现处理的方法. 现实中经常出现一些内容比较长的文本,为了使整体布局美观,需要将文本内容控制在一行 ...

  6. iOS iTunes文件共享

    众所周知苹果不允许用户查看文件,不同的应用之间文件也没有联系.从电脑上往手机传文件,也只能通过开放沙盒目录,传到对应的应用下. 有时候我们需要导入文件到应用沙盒下,或者从沙盒中导出文件.这就需要应用的 ...

  7. Ubuntu_14.04安装docker

    Ubuntu_14.04安装docker $ sudo apt-get update $ sudo apt-get install apt-transport-https ca-certificate ...

  8. javee学习-通过ServletContext对象实现数据共享

    1,设置值. ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象 // ...

  9. EOF是什么?

    转自http://www.ruanyifeng.com/blog/2011/11/eof.html 学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示"文字 ...

  10. 九度OJ 1082 代理服务器 -- 贪心算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1082 题目描述: 使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私.我们知道n个代理服务 ...