zoukankan      html  css  js  c++  java
  • poj 2195 (最小费用最大流)

    赤裸裸的最小费用最大流,不过话说KM 算法当然也可以搞定。

    Going Home
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14859   Accepted: 7604

    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 <iostream>
    using namespace std;
    #define N 10010
    #define INF 0x3fffffff
    
    struct node
    {
        int to,next,w,c;
    }edge[100*N];
    
    
    int cnt,pre[N];
    int n,m;
    char g[110][110];
    int s,t;
    int que[N*1000];
    int point[N],pedge[N];
    
    void add_edge(int u,int v,int w,int c)
    {
        edge[cnt].to=v;
        edge[cnt].w=w;
        edge[cnt].c=c;
        edge[cnt].next=pre[u];
        pre[u]=cnt++;
    }
    
    int spfa()
    {
        int qf=1,qd=0;
        int dis[N];
        int mark[N];
        memset(point,-1,sizeof(point));
        memset(pedge,-1,sizeof(pedge));
        for(int i=0;i<=t;i++)
        {
            dis[i]=INF;
            mark[i]=0;
        }
        dis[s]=0;
        mark[s]=1;
        que[0]=s;
        while(qf>qd)
        {    
            int cur=que[qd++];
            mark[cur]=0;
            for(int p=pre[cur];p!=-1;p=edge[p].next)
            {
                int v=edge[p].to;
                int w=edge[p].w;
                int c=edge[p].c;
                if(edge[p].w==0) continue;
                if( dis[v] > dis[cur]+c )
                {
                    dis[v]=dis[cur]+c;
                    point[v]=cur;
                    pedge[v]=p;
                    if(mark[v]==0)
                    {
                        mark[v]=1;
                        que[qf++]=v;
                    }
                }
            }
        }
        if( dis[t] == INF ) return 0;
        else return 1;
    }
    
    int fuc()
    {
        int sum=0;
        int tmp=t;
        while(tmp!=s)
        {
            int p=pedge[tmp];
            edge[p].w -= 1;
            edge[p^1].w += 1;
            sum+=edge[p].c;
            tmp=point[tmp];
        }
        return sum;
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m)&&(n+m))
        {
            cnt=0;
            s=0;
            t=n*m+1;
            memset(pre,-1,sizeof(pre));
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++)
                {
                    int id=(i-1)*m+j;
                    cin>>g[i][j];
                    if( g[i][j]=='m' )
                    {
                        add_edge(s,id,1,0);
                        add_edge(id,s,0,0);
                    }
                    if(g[i][j]=='H')
                    {
                        add_edge(id,t,1,0);
                        add_edge(t,id,0,0);
                    }
                    if(i!=1)
                    {
                        add_edge(id,id-m,INF,1);
                        add_edge(id-m,id,0,-1);
                    }
                    if(i!=n)
                    {
                        add_edge(id,id+m,INF,1);
                        add_edge(id+m,id,0,-1);
                    }
                    if(j!=1)
                    {
                        add_edge(id,id-1,INF,1);
                        add_edge(id-1,id,0,-1);
                    }
                    if(j!=m)
                    {
                        add_edge(id,id+1,INF,1);
                        add_edge(id+1,id,0,-1);
                    }
                }
            int sum=0;
            while(spfa())
            {
                sum+=fuc();
            }
            printf("%d\n",sum);
        }
        return 0;
    }
  • 相关阅读:
    修改mysql root 密码
    web.xml中contextConfigLocation的作用
    项目中提示找不到class,跟命名规则有关系RulesConfigDao
    myBatis抛出异常Result Maps collection already contains value ...
    mysql 远程访问授权
    maven项目依赖小试牛刀+私库
    eclipse中复制项目更名注意事项
    在eclipse中的tomcat内存设置
    广告宣传单页制作的注意事项
    Linux系统的负载与CPU、内存、硬盘、用户数监控的shell脚本
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/2951601.html
Copyright © 2011-2022 走看看