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. Nginx高性能服务器安装、配置、运维 (1) —— Nginx简介

    一.Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,同时也是一个 IMAP/POP3/SMTP 代理服务器. Nginx特点 ...

  2. JVM笔记4:Java内存分配策略

    简单来说,对象内存分配主要是在堆中分配.但是分配的规则并不是固定的,取决于使用的收集器组合以及JVM内存相关参数的设定 以下介绍几条基本规则(使用的ParNew+Serial Old收集器组合): 一 ...

  3. 迭代器模式(Iterator Pattern)

    迭代器模式定义:Iterator Pattern提供一种方法顺序访问一个聚合元素中的各个元素,而又不暴漏内部方法 酒吧提供beer和wine: public class Bar { private L ...

  4. php 5.3+ 连接mssql

    php5.3+里已经没有mssql的dll扩展了,需要使用SQL Server Driver for PHP 这里有两个版本有两个版本支持不同的php版本. 1.SQL Server Driver f ...

  5. DevExpress GridControl 列中显示图片

    一.GridControl 的Columns中添加列 1.列名:FieldName命名为img 2.类型:ColumnEdit属性中 选择PictureEdit类型(RepositoryItemPic ...

  6. Es6 之for of

    能工摹形,巧匠窃意. -- 毕加索 2016-10-10 <!DOCTYPE HTML> <html> <head> <script src="tr ...

  7. js一些算法实现

    1.约瑟夫环实现 //附有调试 function joseph(n,p){ var arr=[]; for(var i=0;i<n;i++){ arr.push(i); } debugger; ...

  8. MathType支持64位 WIN 7Office 2013(完美解决)(转载)

    经过几次尝试解决了,方法如下: 1. 安装MathType 6.8 (别的版本不知是否适用,本人安装的是该版本) 2. 将以下两个文件拷贝出来 C:\Program Files (x86)\MathT ...

  9. (一)问候Hibernate4

    第一节:Hibernate 简介 官网:http://hibernate.org/ Hibernate 是一个开放源代码的对象关系映射框架,它对JDBC 进行了非常轻量级的对象封装,使得Java程序员 ...

  10. 利用switch语句计算特定的年份的月份共有几天。

    //利用switch语句计算特定的年份的月份共有几天. let year =2015 let month =2 //先判断闰年中二月份的情况 ifmonth ==2 { if (year %400 = ...