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 (最小费用最大流)的更多相关文章

  1. POJ2195&&HDU1533(KB11-D 最小费用最大流)

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23515   Accepted: 11853 Desc ...

  2. POJ2195 Going Home —— 最大权匹配 or 最小费用最大流

    题目链接:https://vjudge.net/problem/POJ-2195 Going Home Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. 最小费用最大流 POJ2195-Going Home

    网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn. ...

  4. POJ 2195 Going Home 【最小费用最大流】

    题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:2715 ...

  5. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  6. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  7. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  8. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  9. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

随机推荐

  1. ssh 远程命令

    远程拷贝文件,scp -r 的常用方法: 1.使用该命令的前提条件要求目标主机已经成功安装openssh-server 如没有安装使用 sudo apt-get install openssh-ser ...

  2. Teen Readers【青少年读者】

    Teen Readers Teens and younger children are reading a lot less for fun, according to a Common Sense ...

  3. PAT-B1032

    1032 挖掘机技术哪家强(20) 输入格式: 输入在第1行给出不超过10^5^的正整数N,即参赛人数.随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号).及其比赛 ...

  4. JAVA学习一 对象数组

    对象数组 今天在写一个代码,才发现自己对于对象数组的理解是不够的,那么就讲讲自己现在的理解. 对于数组中的每一个元素都是一个针对对象的引用 他会指向你的具体的一个堆上的对象,它本身知识一个地址值,与其 ...

  5. 38-JWT 设计解析及定制

    可去官网下载Security项目查看源码 只需修改 AddJwtBearer中的行为即可 public void ConfigureServices(IServiceCollection servic ...

  6. Python标准库--inspect

    inspect模块是针对模块,类,方法,功能等对象提供些有用的方法.例如可以帮助我们检查类的内容,检查方法的代码,提取和格式化方法的参数等. import inspect import os clas ...

  7. [KAFKA]kafka常用操作

    -- kafka路径示例 /opt/cloudera/parcels/KAFKA/bin-- kafka启动./kafka-server-start.sh -daemon ../config/serv ...

  8. 如何从“点子”落地到“执行”?—完整解析1个手游传播类mini项目的进化

    本文来自网易云社区 作者:林玮园 从点子到落地,是不确定到确定的过程,是从模糊概念到具体现实的实现过程.无论什么点子,在落地变现的过程中都会有很多疑问产生. 首先,不确定点子本身是否成立.点子的背后是 ...

  9. 关于transition动画下,如果有fixed元素,渲染的奇葩问题

    之前我们机票页面有生成一个低价日历,然后我们有一个需求就是滚动到那个月份,对应显示这个月,然后这个区域是fixed定位的,然后奇怪的事情发生了,就是低价日历的动画执行完后,修改页面的html却没有正常 ...

  10. 《机器学习实战》 in python3.x

    机器学习实战这本书是在python2.x的环境下写的,而python3.x中好多函数和2.x中的名称或使用方法都不一样了,因此对原书中的内容需要校正,下面简单的记录一下学习过程中fix的部分 1.pr ...