zoukankan      html  css  js  c++  java
  • POJ 2195 Going Home

    Going Home
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15130   Accepted: 7740

    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

       考察点: 最小费用最大流
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    struct num
    {
        int x,y;
    }h[200],m[200];
    int cost[300][300],cap[300][300];
    int queue[1000000],status[300],dis[300],pre[300];
    int INF=0x7fffffff,sta,end;
    int main()
    {
        int EK();
        int i,j,n,u,s,t,top1,top2,x1,y1,x2,y2;
        char c;
        while(scanf("%d %d%*c",&n,&u)!=EOF)
        {
            if(n==0&&u==0)
            {
                break;
            }
            top1=top2=0;
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=u;j++)
                {
                    scanf("%c",&c);
                    if(c=='H')
                    {
                        h[top1].x=i;
                        h[top1++].y= j;
                    }else if(c=='m')
                    {
                        m[top2].x= i;
                        m[top2++].y= j;
                    }
                }
                getchar();
            }
            if(top1==0)
            {
                printf("0\n");
                continue;
            }
            memset(cap,0,sizeof(cap));
            memset(cost,0,sizeof(cost));
            n=top1;
            for(i=1;i<=n;i++)
            {
                cap[0][i]=1;
            }
            for(i=1;i<=n;i++)
            {
                x1=m[i-1].x; y1=m[i-1].y;
                for(j=1;j<=n;j++)
                {
                    x2=h[j-1].x; y2=h[j-1].y;
                    x2= x2- x1;
                    if(x2<0)
                    {
                        x2=-x2;
                    }
                    y2=y2-y1;
                    if(y2<0)
                    {
                        y2=-y2;
                    }
                    cap[i][n+j]=1;
                    cost[i][n+j]=(x2+y2);
                    cost[n+j][i]=-(x2+y2);
                }
            }
            for(i=1;i<=n;i++)
            {
                cap[n+i][n+n+1]=1;
            }
            s=0;
            sta=0; end=2*n+1;
            s=EK();
            printf("%d\n",s);
        }
        return 0;
    }
    int bfs()
    {
        int i,j,base,top,x;
        base=top=0;
        for(i=0;i<=end;i++)
        {
            dis[i]=INF;
        }
        memset(status,0,sizeof(status));
        queue[top++]=0;
        status[0]=1;
        dis[0]=0;
        while(base<top)
        {
            x=queue[base++];
            status[x]=0;
            for(i=1;i<=end;i++)
            {
                if(x!=i&&cap[x][i])
                {
                    if(dis[i]>(dis[x]+cost[x][i]))
                    {
                        dis[i]=dis[x]+cost[x][i];
                        pre[i]=x;
                        if(!status[i])
                        {
                            status[i]=1;
                            queue[top++]=i;
                        }
                    }
                }
            }
        }
        if(dis[end]==INF)
        {
            return 0;
        }else
        {
            return 1;
        }
    }
    int EK()
    {
        int i,j,s=0,k,min=INF;
        while(1)
        {
            k=bfs();
            if(k==0)
            {
                break;
            }
            for(i=end; i!=0; i=pre[i])
            {
                j=pre[i];
                if(min>cap[j][i])
                {
                    min=cap[j][i];
                }
            }
            for(i=end; i!=0; i=pre[i])
            {
                j=pre[i];
                s+=(cost[j][i]*min);
                cap[j][i]-=min;
                cap[i][j]+=min;
            }
        }
        return s;
    }
    

  • 相关阅读:
    极客互动极客技术专题【003期】:java mvc 增删改查 自动生成工具来袭
    协议命令网络工程试验一
    主题网站分享两套免费的超棒响应式HTML5网站模板
    算法结点图的多源点最短路问题和传递闭包之FloydWarshall算法 By ACReaper
    属性页面Flexbox布局的简单演示之二
    数据库性能Quest Performance Analysis Overview
    网站查看帮助查看本地表单元素样子的网站 Native Form Elements
    文件格式配置文件weka频繁模式挖掘使用方法
    风格希望分享8个超棒的免费界面UI设计
    方法事务applicationContext.xml
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3029475.html
Copyright © 2011-2022 走看看