zoukankan      html  css  js  c++  java
  • hdu-1533 Going Home(最小费用最大流)

    题目链接:

    Going Home

    Time Limit: 10000/5000 MS (Java/Others)  

      Memory Limit: 65536/32768 K (Java/Others)


    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
     
    题意:
     
    每个人找到一个房子,使他们路径长度和最小;
     
    思路:
     
    每个人和每个房子之间有一条容量为1的边,花费为哈夫曼距离;设一个源点和一个汇点;源点与每个人之间有容量为1花费为0的边,每个房子与汇点之间有容量为1花费为0的边;然后跑一遍最小费用最大流就出来答案了;还有就是二分图匹配也可以做;
     
    AC代码;
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    
    using namespace std;
    
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    
    typedef  long long LL;
    
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=1e5+10;
    const int maxn=200+10;
    const double eps=1e-8;
    
    char mp[110][110];
    int cost[2*maxn][2*maxn],minflow[2*maxn],num,dis[2*maxn],vis[2*maxn],path[2*maxn],flow[2*maxn][2*maxn];
    queue<int>qu;
    struct node
    {
    	int x,y;
    }house[110],man[110];
    int spfa(int s,int e)
    {
    	For(i,1,num)minflow[i]=dis[i]=inf;
    	mst(vis,0);
    	qu.push(s);vis[s]=1;
    	dis[s]=0;path[s]=-1;
    	while(!qu.empty())
    	{
    		int fr=qu.front();
    		vis[fr]=0;qu.pop();
    		for(int i=1;i<=num;i++)
    		{
    			if(dis[i]>dis[fr]+cost[fr][i]&&flow[fr][i])
    			{
    				dis[i]=dis[fr]+cost[fr][i];
    				path[i]=fr;
    				minflow[i]=min(minflow[i],flow[fr][i]);
    				if(!vis[i]){qu.push(i);vis[i]=1;}
    			}
    		}
    	}
    	if(dis[e]==inf)return 0;
    	return 1;
    }
    
    
    inline int MCMF(int s,int e)
    {
    	int ans=0;
    	while(spfa(s,e))
    	{
    		int temp=minflow[e];
    		ans+=dis[e];
    		int cur=e,fa=path[cur];
    		while(fa!=-1)
    		{
    			fa=path[cur];
    			flow[fa][cur]-=temp;
    			flow[cur][fa]+=temp;
    			cur=fa;
    		}
    	}
    	return ans;
    }
    
    
    int main()
    {
    		int n,m;
    		while(1)
    		{
    			read(n);read(m);
    			if(!n&&!m)break;
    			For(i,1,n)scanf("%s",mp[i]+1);
    			int cnt1=0,cnt2=0;
    			For(i,1,n)
    			{
    				For(j,1,m)
    				{
    					if(mp[i][j]=='H')
    					{
    						++cnt2;
    						house[cnt2].x=i;
    						house[cnt2].y=j;
    					}
    					else if(mp[i][j]=='m')
    					{
    						++cnt1;
    						man[cnt1].x=i;
    						man[cnt1].y=j;
    					}
    				}
    			}
    			num=cnt1*2+2;
    			int s=num-1,e=num;
    			For(i,1,num)For(j,1,num)cost[i][j]=inf,flow[i][j]=0;
    
    			For(i,1,cnt1)
    			{
    				For(j,1,cnt1)
    				{
    					cost[i][j+cnt1]=abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y);
    					cost[j+cnt1][i]=-cost[i][j+cnt1];
    					flow[i][j+cnt1]=1;
    				}
    			}
    			For(i,1,cnt1)
    			{
    				flow[s][i]=1;
    				cost[s][i]=0;
    				cost[i][s]=0;
    				flow[i+cnt1][e]=1;
    				cost[i+cnt1][e]=0;
    				cost[e][i+cnt1]=0;
    			}
    			cout<<MCMF(s,e)<<"
    ";
    		}
            return 0;
    }
    

      

  • 相关阅读:
    WebView中实现文件下载功能
    PrograssBar的setIndeterminateDrawable不起作用
    62个Android Studio小技巧合集
    Touch 事件的分发和消费机制
    AndroidStudio
    pagefile.sys and heberfil.sys
    android ANR产生原因和解决办法
    JAVA中分为基本数据类型及引用数据类型
    handler
    洛谷 P2709 小B的询问
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5711030.html
Copyright © 2011-2022 走看看