hdoj--1533--Going Home(最小费用流)
来源:互联网 发布:万得金融终端mac版 编辑:程序博客网 时间:2024/05/18 10:52
Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3800 Accepted Submission(s): 1948
Total Submission(s): 3800 Accepted Submission(s): 1948
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.
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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
21028
Source
Pacific Northwest 2004
Recommend
lwg | We have carefully selected several similar problems for you: 1532 3416 3491 3338 3081
#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<queue>#include<algorithm>using namespace std;#define MAXN 200+10#define MAXM 80000+100#define INF 0x3f3f3fstruct node{int from,to,cap,flow,cost,next;}edge[MAXM];int head[MAXN],top,pre[MAXN];int dis[MAXN],vis[MAXN],n,m,source,sink;void init(){top=0;memset(head,-1,sizeof(head));}struct Node {int x,y;};Node H[MAXN],M[MAXN];int m_cnt,h_cnt;void add(int u,int v,int w,int c){node E1={u,v,w,0,c,head[u]};edge[top]=E1;head[u]=top++;node E2={v,u,0,0,-c,head[v]};edge[top]=E2;head[v]=top++;}int dist(int x1, int y1, int x2, int y2){ return abs(x1 - x2) + abs(y1 - y2);}void getmap(){m_cnt=h_cnt=0;char str[110][110];for(int i=0;i<n;i++){scanf("%s",str[i]);for(int j=0;j<m;j++){if(str[i][j]=='m'){++m_cnt;M[m_cnt].x=i;M[m_cnt].y=j;}if(str[i][j]=='H'){h_cnt++;H[h_cnt].x=i;H[h_cnt].y=j;}}}int k=m_cnt;source=0;sink=2*k+1;for(int i=1;i<=k;i++){add(source,i,1,0);add(i+k,sink,1,0);for(int j=1;j<=k;j++){int d=dist(H[i].x,H[i].y,M[j].x,M[j].y);add(i,j+k,1,d);}}}bool SPFA(int s,int t){queue<int>q;memset(dis,INF,sizeof(dis));memset(vis,0,sizeof(vis));memset(pre,-1,sizeof(pre));dis[s]=0;vis[s]=1;q.push(s);while(!q.empty()){int u=q.front();q.pop();vis[u]=false;for(int i=head[u];i!=-1;i=edge[i].next){node E=edge[i];if(dis[E.to]>dis[u]+E.cost&&E.cap>E.flow){dis[E.to]=dis[u]+E.cost;pre[E.to]=i;if(!vis[E.to]){vis[E.to]=true;q.push(E.to);}}}}return pre[t]!=-1;}void MCMF(int s,int t,int &cost,int &flow){flow=0;cost=0;while(SPFA(s,t)){int Min=INF;for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){node E=edge[i];Min=min(Min,E.cap-E.flow);}for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){edge[i].flow+=Min;edge[i^1].flow-=Min;cost+=edge[i].cost*Min;}flow+=Min;//printf("%d\n",cost);}}int main(){while(scanf("%d%d",&n,&m),n|m){init();getmap();int cost,flow;MCMF(source,sink,cost,flow);printf("%d\n",cost);}return 0;}
0 0
- hdoj--1533--Going Home(最小费用流)
- hdoj 1533 Going Home(EK增强版求最小费用最大流)
- hdu 1533 Going Home 最小费用流
- HDU 1533 Going Home 最小费用流
- Going Home (hdu 1533 最小费用流)
- HDU 1533 Going Home(最小费用流)
- hdu 1533 Going Home (最小费用流)
- poj_2195 Going Home(最小费用流)
- POJ2195-Going Home(最小费用流)
- HDU1533:Going Home(最小费用流)
- HDOJ 1533.Going Home 最小费用最大流
- hdu 1533 going home (最小费用最大流)
- Going Home----最小费用流
- Going Home(最小费用流)
- hdoj 1533 Going Home 【最小费用最大流】【KM入门题】
- hdu 1533 Going Home【最大流最小费用流】
- hdu 1533 || poj 2195 Going Home (最小费用最大流)
- POJ 2195 & HDU 1533 Going Home(最小费用最大流)
- POJ 2635 The Embarrassed Cryptographer(素数线性筛法)
- 安娜·埃贝尔《夜》
- 【小作品】STM32无线WIFI视频小车制作剖析(上)
- Leet Code 136 Single Number
- Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
- hdoj--1533--Going Home(最小费用流)
- HDU1002
- 1026. Table Tennis (30)
- Map 集大成者
- XMPP协议了解
- 一切成功源于积累——20160211 黄金单日涨幅 1197-1160 超5%
- 货郎担问题(TSP)
- POJ 2640 Playground(多边形判定)
- 1027. Colors in Mars (20)