博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2195——Going Home(最小费用最大流)
阅读量:2345 次
发布时间:2019-05-10

本文共 2731 字,大约阅读时间需要 9 分钟。

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

看了两天的最小费用最大流,算是看懂了点皮毛。

这个算法顾名思义,就是在流量最大的前提下,达到所用的费用最小的要求。这题最小费用就是最短距离。
大致就是先用SPFA求出最短路径,该路径就是增广链上费用最小的一条,再在这条链上找最大流。
再就是调整容量,直到找不到增广路径为止,最后求得的最小费用就是答案。
建图的过程:
超级源点连接人,容量为1,代价为0;
人连接房屋,容量为1,代价为曼哈顿距离(最短距离);
房屋连接超级汇点,容量为1,代价为0。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 1005#define Mod 10001using namespace std;struct Node{ int x,y;};Node man[MAXN],house[MAXN];int n,m,source,ending,mincost,maxflow;int res[MAXN][MAXN],cost[MAXN][MAXN],parent[MAXN],d[MAXN];int need[MAXN],have[MAXN],nn[MAXN][MAXN],hh[MAXN][MAXN];void spfa(){ queue
q; bool vis[MAXN]; memset(vis,false,sizeof(vis)); memset(parent,-1,sizeof(parent)); for(int i=0; i<=ending; ++i) d[i]=INF; q.push(0); d[0]=0; vis[0]=true; while(!q.empty()) { int v=q.front(); q.pop(); vis[v]=false; for(int i=0; i<=ending; ++i) { if(res[v][i]&&d[v]+cost[v][i]
>s; if(s=='m') { man[num1].x=i; man[num1++].y=j; } if(s=='H') { house[num2].x=i; house[num2++].y=j; } } source=0,ending=num1+num2+1; memset(res,0,sizeof(res)); memset(cost,0,sizeof(cost)); for(int i=0; i

转载地址:http://gvcvb.baihongyu.com/

你可能感兴趣的文章
删除链表中重复的节点——重复节点不保留
查看>>
2018腾讯校招编程题——最重要的城市
查看>>
删除链表中重复的节点——重复节点保留一个
查看>>
实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit()).md
查看>>
链表排序.md
查看>>
进程与线程的区别与联系、进程与线程的通信方式
查看>>
C++与C的区别
查看>>
产生死锁的必要条件及处理方法
查看>>
TCP和UDP的区别
查看>>
事务具有四个特性
查看>>
Hadoop Hdfs 配置
查看>>
tsung集群测试
查看>>
oracle定时删除表空间的数据并释放表空间
查看>>
解决文件提示: /bin/ksh^M: bad interpreter: bad interpreter:No such file or directory
查看>>
ajaxanywhere jsp 使用
查看>>
jquery的使用
查看>>
如何静态化JSP页面
查看>>
XML 与 Java 技术: 用 Castor 进行数据绑定
查看>>
Python未知领域系列:(附Python学习教程+Python学习路线)Python高级教程之面向对象
查看>>
盘点Python 面向对象编程最容易被忽视的知识点
查看>>