Hdu1533



Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3947    Accepted Submission(s): 2025


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.
Hdu1533_第1张图片
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里,每走一步的代价是1,问最小花费?
将m编号为1至mcnt,将h编号为mcnt+1至mcnt+hcnt(mcnt,hcnt为m和h的数量),
将每个m和每个h相连,cost为m到h的步数,流量为1。
代码:
#include <stdio.h> #include <iostream> #include <string.h> #include<cmath> using namespace std; const int N=300; const int MAXE=200000; const int inf=1<<30; int head[N],ep; int d[N],pre[N]; bool vis[N]; int q[MAXE]; struct Edge {     int u,v,c,w,next; }edge[MAXE]; void addedge(int u,int v,int w,int c)//u v 费用 容量 {     edge[ep].u=u;     edge[ep].v=v;     edge[ep].w=w;     edge[ep].c=c;     edge[ep].next=head[u];     head[u]=ep++;     edge[ep].v=u;     edge[ep].u=v;     edge[ep].w=-w;     edge[ep].c=0;     edge[ep].next=head[v];     head[v]=ep++; } int SPFA(int src,int des) {     int l,r;     memset(pre,-1,sizeof(pre));     memset(vis,0,sizeof(vis));     for(int i=0;i<=des;i++) d[i]=inf;     d[src]=0;     l=0;r=0;     q[r++]=src;     vis[src]=1;     while(l<r)     {         int u=q[l++];         vis[u]=0;         for(int j=head[u];j!=-1;j=edge[j].next)         {             int v=edge[j].v;             if(edge[j].c>0&&d[u]+edge[j].w<d[v])             {                 d[v]=d[u]+edge[j].w;                 pre[v]=j;                 if(!vis[v])                 {                     vis[v]=1;                     q[r++]=v;                 }             }         }     }     if(d[des]==inf)         return 0;     return 1; } int MCMF(int src,int des) {     int flow=0,ans=0;//flow是得到的最大流的值 ans得到的是最小的费用     while(SPFA(src,des))     {         ans+=d[des];         int u=des;         int mini=inf;         while(u!=src)         {             if(edge[pre[u]].c<mini)                 mini=edge[pre[u]].c;                 u=edge[pre[u]].u;         }         flow+=mini;         u=des;         while(u!=src)         {             edge[pre[u]].c-=mini;             edge[pre[u]^1].c+=mini;             u=edge[pre[u]].u;         }     }     return ans; } ///以上为模板 struct man {     int x;     int y; }M[111]; struct house {     int x,y; }H[111]; char str[111]; int main() {     int n,m,mcnt,hcnt,i,j,src,des;     while(scanf("%d%d",&n,&m)!=EOF)     {         if(!n&&!m) break;         ep=0;         memset(head,-1,sizeof(head));         mcnt=hcnt=0;         for(i=0;i<n;i++)         {             scanf("%s",str);             for(j=0;j<m;j++)             {                 if(str[j]=='H')                 {                     hcnt++;                     H[hcnt].x=i; H[hcnt].y=j;                 }                 else if(str[j]=='m')                 {                     mcnt++;                     M[mcnt].x=i; M[mcnt].y=j;                 }             }         }         for(i=1;i<=mcnt;i++)         {             for(j=1;j<=hcnt;j++)             {                 int dis=abs(M[i].x-H[j].x)+abs(M[i].y-H[j].y);                 addedge(i,mcnt+j,dis,1);             }         }         src=0;         des=mcnt+hcnt+1;         for(i=1;i<=mcnt;i++)           addedge(src,i,0,1);         for(j=1;j<=hcnt;j++)             addedge(mcnt+j,des,0,1);         int ans=MCMF(src,des);
        printf("%d\n",ans);     }     return 0; }
 

你可能感兴趣的:(Hdu1533)