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 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
随机推荐
- 密码发生器 南阳acm519
密码发生器 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如 ...
- docker学习(三) 安装docker的web可视化管理工具
1.docker是一个一款很轻便的应用容器引擎,为了更好的管理和使用docker,使用web可视化管理工具似乎更符合大多数人的需求.在这里,我给大家分享下自己使用过的几款web工具:docker UI ...
- Django信号的使用
https://www.cnblogs.com/renpingsheng/p/7566647.html
- 海思NB-IOT的SDK添加AT指令方法
1. 首先打开app_at_cmd_parse.c文件,在文件的末尾找到,可在中间添加需要的代码 /查询电信自注册结果 {AT_FLAG_VISIABLE | AT_FLAG_LOGABLE, &qu ...
- 你了解的UIKit结构?
- Python 3基础教程21-列表和元组
本文介绍列表也元组,先来看看他们的定义. # 元组和列表 # 元组的定义 x = 5,6,2,6 # 或者这样写 x = (5,6,2,6) # 列表定义 y = [5,6,2,6] # 元组的使用, ...
- python 基础篇 09 函数初识
<<<<<<<<<<<<<<<------------------------------函 ...
- cut 与 awk
cut和awk都能分割显示需要的内容 但在需要以空格为分隔符的情况下: # free -m|grep Mem Mem: cut是以单一空格为分隔符的: # free -m|grep Mem|cut - ...
- deeplearning.ai课程学习(2)
第二周:神经网络的编程基础(Basics of Neural Network programming) 1.逻辑回归的代价函数(Logistic Regression Cost Function) 逻 ...
- HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)
Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...