POJ-2195 Going Home(最小费用最大流)
来源:互联网 发布:linux ipython 编辑:程序博客网 时间:2024/05/21 06:49
Going Home
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21918 Accepted: 11087
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
题意:在一个N*M的矩阵中,有n个人和房子,每个人每走一步的费用为1,每个房子只能住1个人,问所有人都住进房子的最小花费是多少
题解:最小费用最大流,把第1~n个人编号为1~n,第1~n个房子编号为1+n~2n,设源点和汇点编号分别为0和2n+1,将源点与1~n连一条费用为0容量为1的有向边,将n+1~2n与汇点连一条费用为0容量为1的有向边,将i∈[1,n]与j∈[n+1,2n]连一条费用为|x2-x1|+|y2-y1|容量为1的有向边.①费用不难理解,人与房子间的费用肯定等于两者间的最短距离;②为什么边的容量为1?注意每个房子只能住1个人,也就对应着该点的入度和出度等于1.
#include<cstdio>#include<algorithm>#include<string.h>#include<vector>#include<math.h>#include<stdlib.h>#include<queue>using namespace std;typedef pair<int,int>PII;const int inf = 0x3f3f3f3f;const int maxn = 205;char st[maxn];vector<PII>v1,v2;struct Edge{ int w,c;}edge[maxn][maxn];int pre[maxn],vis[maxn],d[maxn],n,m;bool spfa(int s,int t){ queue<int>q; memset(vis,0,sizeof(vis)); memset(d,0x3f,sizeof(d)); memset(pre,-1,sizeof(pre)); q.push(s); vis[s]=1; d[s]=0; while(!q.empty()){ int u=q.front();q.pop(); vis[u]=0; for(int v=s;v<=t;v++){ if(edge[u][v].w>0&&d[v]>d[u]+edge[u][v].c){ d[v]=d[u]+edge[u][v].c; pre[v]=u; if(!vis[v]){ vis[v]=1; q.push(v); } } } } return ~pre[t];}int MinCostMaxFlow(int s,int t,int& cost){ int flow=0; cost=0; while(spfa(s,t)){ int minFlow=inf,v=t,u=pre[t]; while(~u){ minFlow=min(minFlow,edge[u][v].w); v=u; u=pre[v]; } v=t;u=pre[t]; while(~u){ edge[u][v].w-=minFlow; edge[v][u].w+=minFlow; cost+=edge[u][v].c*minFlow; v=u; u=pre[v]; } flow+=minFlow; } return flow;}int main(){ // freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m),n+m){ memset(edge,-1,sizeof(edge)); v1.clear(); v2.clear(); for(int i=0;i<n;i++){ scanf("%s",st); for(int j=0;j<m;j++){ if(st[j]=='m') v1.push_back(PII(i,j)); else if(st[j]=='H') v2.push_back(PII(i,j)); } } n=v1.size(); for(int i=0;i<n;i++) { edge[0][i+1].w=1; edge[0][i+1].c=0; edge[i+1][0].w=0; edge[i+1][0].c=0; edge[i+n+1][2*n+1].w=1; edge[i+n+1][2*n+1].c=0; edge[2*n+1][i+n+1].w=0; edge[2*n+1][i+n+1].c=0; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ edge[i+1][j+n+1].w=1; edge[i+1][j+n+1].c=abs(v1[i].first-v2[j].first)+abs(v1[i].second-v2[j].second); edge[j+n+1][i+1].w=0; edge[j+n+1][i+1].c=-edge[i+1][j+n+1].c; } } int ans=0; MinCostMaxFlow(0,2*n+1,ans); printf("%d\n",ans); } return 0;}
0 0
- poj-2195-Going Home最小费用最大流
- poj - 2195 - Going Home(最小费用最大流)
- hdu 1533 || poj 2195 Going Home (最小费用最大流)
- POJ 2195 Going Home 最小费用最大流
- POJ 2195 Going Home 最小费用最大流
- poj 2195 Going Home(最小费用最大流)
- POJ 2195 Going Home (最小费用最大流)
- 【最小费用最大流】POJ-2195 Going Home
- POJ 2195 & HDU 1533 Going Home(最小费用最大流)
- poj 2195 Going Home(最小费用最大流)
- POJ 2195 Going Home -- 最小费用最大流模板
- poj 2195 Going Home【最小费用最大流】
- Going Home POJ 2195 (最小费用最大流)
- POJ 2195 Going Home (最小费用最大流)
- 最小费用最大流-POJ-2195-Going Home
- poj-2195-Going Home(最小费用最大流模板题)
- POJ-2195 Going Home(最小费用最大流)
- POJ 2195Going Home(最小费用最大流)
- Codeforces 118D Caesar's Legions【dp】好题
- 27. Remove Element
- 查找算法——找到序列中第二大的数
- 加速审核网址
- Android城市选择列表(一)——RecyclerView数据分组
- POJ-2195 Going Home(最小费用最大流)
- 转发保留不错一篇 数学模型神经网络在程序化交易模型构建中的运用探讨
- 常对象常引用等的一些理解
- 机器学习笔记
- 第一周 从问题到C语言程序设计的程序
- ROS学习-URDF学习纪要
- 妈妈是影响我最深的人,也是我最想逃离的人
- SPOJ DISUBSTR - Distinct Substrings(后缀数组[不相同的子串的个数])
- is missing required source folder:' src'