POJ2195:Going Home (最小费用最大流)
Going Home
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 26212 | Accepted: 13136 |
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289
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
题意:
给出一个n*m的图,上面有数量相等的人和房子,同时有些空地,每个人每走一步都要给其1的金币,问当最后所有人都到房子时最小给出的金币是多少。
题解:
我一开始以为会BFS求每个人到房子的最短路径,后来发现没有障碍,直接人到房子的曼哈顿距离即是最短路径。这也就是每个人到相应房子应给的金钱。
因为一个房子只能容纳下一个人,考虑边权为1的最大流,然后跑个最小费用最大流就行了。
这里将每个人与所有房子连边,费用为其到房子的最短距离,边容量都为1。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
using namespace std; const int N = ,INF = ,t = ;
int n,m,cnt,tot,num;
int map[N][N],d[N][N],head[N*N],dis[N*N],vis[N*N],a[N*N],p[N*N],pre[N*N];
int Dis(int x1,int y1,int x2,int y2){
return abs(x1-x2)+abs(y1-y2);
}
struct Edge{
int u,v,next,c,w;
}e[N*N*N];
void adde(int u,int v,int w){
e[tot].v=v;e[tot].c=;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
e[tot].v=u;e[tot].c=;e[tot].w=-w;e[tot].next=head[v];head[v]=tot++;
}
int BellmanFord(int s,int t,int &flow,int &cost){
for(int i=;i<=t+;i++) dis[i]=INF,a[i]=INF;
queue <int> q;memset(vis,,sizeof(vis));memset(p,-,sizeof(p));
memset(pre,-,sizeof(pre));
q.push(s);vis[s]=;dis[s]=;p[s]=;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(e[i].c> &&dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;
a[v]=min(a[u],e[i].c);
p[v]=u;
pre[v]=i;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
if(dis[t]==INF) return ;
flow+=a[t];
cost+=a[t]*dis[t];
for(int i=t;i>;i=p[i]){//
e[pre[i]].c-=a[t];
e[pre[i]^].c+=a[t];
}
return ;
}
int Min_cost(){
int flow=,cost=;
while(BellmanFord(,t,flow,cost));
return cost;
}
int main(){
while(~scanf("%d%d",&n,&m)){
if(n== && m==) break;
tot=;num=;cnt=;
memset(head,-,sizeof(head));
vector <pair<int,int> > h;
memset(map,,sizeof(map));
for(int i=;i<=n;i++){
char s[N];
scanf("%s",s);
for(int j=;j<m;j++){
if(s[j]=='H'){
map[i][j+]=;
h.push_back(make_pair(i,j+));
}
if(s[j]=='m') map[i][j+]=,num++;
}
}
for(int i=;i<=num;i++) adde(,i,);
for(int i=num+;i<=*num;i++) adde(i,t,);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(map[i][j]!=) continue ;
cnt++;int tmp=;
for(int k=;k<h.size();k++){
tmp++;
pair <int,int> v = h[k];
adde(cnt,num+tmp,Dis(i,j,v.first,v.second));
}
}
}
printf("%d\n",Min_cost());
}
return ;
}
POJ2195:Going Home (最小费用最大流)的更多相关文章
- POJ2195&&HDU1533(KB11-D 最小费用最大流)
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23515 Accepted: 11853 Desc ...
- POJ2195 Going Home —— 最大权匹配 or 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total ...
- 最小费用最大流 POJ2195-Going Home
网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn. ...
- POJ 2195 Going Home 【最小费用最大流】
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Total Submissions:2715 ...
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- bzoj1927最小费用最大流
其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→ =_=你TM逗我 刚要删突然感觉dinic的模 ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
随机推荐
- ruby 数据类型Symbol
一.符号创建 符号是Symbol类的实例,使用冒号加一个标识符即可创建符号 :a :"This is a symno" 二.符号字符串相互转换 p :symbol.to_s #=& ...
- Matplotlib 基本图表的绘制
图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主 同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsize=None, use_index=T ...
- 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT L.C# As 课程ID,L.score AS 最高分,R.score AS 最低分 FROM SC L ,SC AS R WHERE L.C# = R.C# and L.score = ...
- JENKINS系统的安装部署
JENKINS 安装使用文档 简介 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何类型的构建或持续集成,集成Jenkins可 ...
- Eclipse报错:An internal error occurred during: "Building workspace". Java heap space),卡死解决办法
在项目工程的根目录下,找到.project,用记事本打开,把两处删除掉: 第一处: <buildCommand> <name>org.eclipse.wst.jsdt.core ...
- c/c++容器操作
C++中的容器大致可以分为两个大类:顺序容器和关联容器.顺序容器中包含有顺序容器适配器. 顺序容器:将单一类型元素聚集起来成为容器,然后根据位置来存储和访问这些元素.主要有vector.list.de ...
- 纯js实现复制内容到剪切板
下面的方法可以完美实现: 复制指定input 或者 textarea中的内容: 指定非输入框元素中的内容 代码如下: function copyToClipboard(elem) { // creat ...
- python中的os,shutil模块的定义以及用法
# os 模块 os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Uni ...
- cmd中可以运行java,但不能运行javac命令
在cmd中可以运行java,但运行javac命令时提示:'javac' 不是内部或外部命令,也不是可运行的程序或批处理文件. 原因:安装java时把jdk的路径和jre的路径选择成一样,就造成覆盖了. ...
- 利尔达NB-IOT模块对接移动onenet平台步骤
1. 首先登陆浙江移动onenet网站,http://openiot.zj.chinamobile.com/,进入右上角的开发者中心,然后才能看到创建产品 2. 填写产品的信息,其他信息按照个人实际填 ...