Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2514    Accepted Submission(s): 1269
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
 
题解:把每一个m到H的距离保存起来当作一个边。值记录成负的,由于要求最小距离,常规KM计算的是最大值

#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算法的更多相关文章

  1. hdu 1533 Going Home (KM)

    Going HomeTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. hdu 2255 奔小康赚大钱--KM算法模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 题意:有N个人跟N个房子,每个人跟房子都有一定的距离,现在要让这N个人全部回到N个房子里面去,要 ...

  4. hdu 2853 Assignment KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Last year a terrible earthquake attacked Sichuan ...

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

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

  7. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. HDU 2255 奔小康赚大钱 (KM算法 模板题)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  10. HDU:2255-奔小康赚大钱(KM算法模板)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Mem ...

随机推荐

  1. ZOJ 2110 Tempter of the Bone(DFS)

    点我看题目 题意 : 一个N×M的迷宫,D是门的位置,门会在第T秒开启,而开启时间小于1秒,问能否在T秒的时候到达门的位置,如果能输出YES,否则NO. 思路 :DFS一下就可以,不过要注意下一终止条 ...

  2. HANA内存数据库与oracle数据库的性能比较

    链接: http://wenku.it168.com/redian/hana/ 1.传统磁盘数据库的基本访问模式.为了提高性能在产品和应用之间会加入缓存的内存区域.传统数据库性能瓶颈主要出现在一个是内 ...

  3. spring junit class path resource [ /com/config/spring-core.xml] cannot be opened because it does not exist

    正确写法应该如下: @RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration(locations="classpath: ...

  4. 【HDOJ】3071 Gcd & Lcm game

    刚开始看这个题目,觉得没法做.关键点是数据小于100.因此,可以枚举所有小于100的素因子进行位压缩.gcd就是求最小值,lcm就是求最大值.c++有时候超时,g++800ms.线段树可解. /* 3 ...

  5. 更新你的jar包

    jar文件:/home/resin.jar需更新包中com/caucho/server/port/Port.class类文件 方法1:jar uf resin.jar com/caucho/serve ...

  6. Linux Kernel‘ieee80211_radiotap_iterator_init()’函数拒绝服务漏洞

    漏洞名称: Linux Kernel‘ieee80211_radiotap_iterator_init()’函数拒绝服务漏洞 CNNVD编号: CNNVD-201312-041 发布时间: 2013- ...

  7. nginx错误汇总

    一.Nginx出现413 Request Entity Too Large错误解决方法 Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现, ...

  8. [King.yue]EXT.NET TextFieldFor添加正则表达式

    EXT.NET TextFieldFor添加正则表达式以及提示用法 例:Html.Y().TextFieldFor().Regex("此处添加正则表达式") .InvalidTex ...

  9. 【原】Spark中Client源码分析(二)

    继续前一篇的内容.前一篇内容为: Spark中Client源码分析(一)http://www.cnblogs.com/yourarebest/p/5313006.html DriverClient中的 ...

  10. FCLK PCLK HCLK

    一.对clock的基本认识 1 s3c2410的clock & power management模块包含三个部分:clock control.usb control.power control ...