POJ2195 Going Home(网络流,最小费用流,最小费用路算法)
2018-01-09 11:09
417 查看
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 aline 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 isthe 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
思路
题目给了一个n*m的图.其中:.代表空地
H代表房子
m代表人
人的数量和房子的数量是一样的,现在要让这些人去房子里,每走一步花费的费用为1,问最小要需要多少花费,可以让每个人都进入房子。(房子的容量为1).
首先我们需要建图,把每个人和每个房子的坐标存起来,那么就有l1个人,和l2个房子,建立一个超级源点和超级汇点,给源点和每一个人建立一条容量为1,花费为0的边,给每个房子和汇点建立一个容量为1,花费为0的边。
然后给这些人和这些房子建边。
然后就是最小费用流了
代码
#include <cstdio> #include <cstring> #include <cctype> #include <stdlib.h> #include <string> #include <map> #include <iostream> #include <stack> #include <cmath> #include <queue> #include <vector> #include <algorithm> using namespace std; typedef long long ll; #define inf 1000000 #define mem(a,b) memset(a,b,sizeof(a)) const int N=1000+20; const int M=1000+20; int top,maxflow;//当前边下标,最大流 int dis ,pre ;//源点到点i的最小距离,pre[i]记录前驱 bool vis ;//标记数组 int first ;//存储头结点 struct Edge { int v,next; int cap,flow,cost; } E[M*2]; void init() { mem(first,-1); top=0; maxflow=0; } void add_edge(int u,int v,int c,int cost) { E[top].v=v; E[top].cap=c; E[top].flow=0; E[top].cost=cost; E[top].next=first[u]; first[u]=top++; } void add(int u,int v,int c,int cost) { add_edge(u,v,c,cost); add_edge(v,u,0,-cost); } bool spfa(int s,int t,int n) { int i,u,v; queue<int>q; mem(vis,false); mem(pre,-1); for(int i=1; i<=n; i++) dis[i]=inf; vis[s]=true; dis[s]=0; q.push(s); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=false; for(int i=first[u]; i!=-1; i=E[i].next) { v=E[i].v; if(E[i].cap>E[i].flow&&dis[v]>dis[u]+E[i].cost) { dis[v]=dis[u]+E[i].cost; pre[v]=i; if(!vis[v]) { q.push(v); vis[v]=true; } } } } if(dis[t]==inf) return false; return true; } int MCMF(int s,int t,int n)//minCostMaxFlow { int d; int i,mincost=0;//maxflow当前最大流量,mincost当前最小费用 while(spfa(s,t,n))//表示找到了从s到t的最小费用路 { d=inf; for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //遍历反向边 d=min(d,E[i].cap-E[i].flow); maxflow+=d;//更新最大流 for(int i=pre[t]; i!=-1; i=pre[E[i^1].v]) //增广路上正向边流量+d,反向边流量-d { E[i].flow+=d; E[i^1].flow-=d; } mincost+=dis[t]*d;//dis[t]为该路径上单位流量费用之和 } return mincost; } char s ; struct node { int x,y; } e1 ,e2 ; int main() { int n,m; while(scanf("%d%d",&n,&m)&&(n||m)) { int l1=0,l2=0; init(); for(int i=0; i<n; i++) { scanf("%s",s[i]); for(int j=0; j<m; j++) { if(s[i][j]=='m') e1[++l1].x=i,e1[l1].y=j; if(s[i][j]=='H') e2[++l2].x=i,e2[l2].y=j; } } for(int i=1; i<=l1; i++) add(0,i,1,0); for(int i=1; i<=l2; i++) add(l1+i,l1+l2+1,1,0); for(int i=1; i<=l1; i++) { for(int j=1; j<=l2; j++) { int line=abs(e1[i].x-e2[j].x)+abs(e1[i].y-e2[j].y); add(i,l1+j,1,line); } } printf("%d\n",MCMF(0,l1+l2+1,l1+l2+2)); } return 0; }
相关文章推荐
- POJ2195 Going Home(最小费用最大流模板题)(附用数组建图和用链式前向星建图代码)
- 网络流相关算法总结,最大流EK算法,SAP算法,最小费用最大流,最小费用路算法,最大流最小割定理
- poj2195 Going Home(最大费用最小流)
- POJ 2195 Going Home (网络流之最小费用流)
- tarjan算法入门整理专题(判断是否是一个强连通、通过缩点求至少加几条边让整个图变成强连通和传递的最小费用)
- POJ2195_Going home(最小费用流)
- POJ2195:Going Home(费用流入门)
- 【网络流24题】 No.3 最小路径覆盖问题 (网络流|匈牙利算法 ->最大二分匹配)
- 洛谷-P3381 【模板】最小费用最大流(最小费用路算法)
- HD 1533 Going Home(最小费用最大流模板)
- POJ 2195Going Home(网络流之最小费用流)
- 网络流最大流最小割算法
- HDU 1853Cyclic Tour(网络流之最小费用流)
- (教你彻底理解)网络流:基本概念与算法 最大流最小割
- POJ训练计划2195_Going Home(网络流/费用流)
- POJ 2195Going Home(网络流之最小费用流)
- POJ 2195 Going Home(网络流-费用流)
- HDU 3488Tour(流的最小费用网络流)
- poj2195 Going Home(费用流|KM)
- 最小费用网络流,不含重边