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字符串的一些方法
最近因为公司需求开始看ruby,先从ruby的基本数据类型开始看 看到ruby的字符串类型string,发现ruby中的字符串单双引号是不一样的,这点和Python有那么点不一样 主要是我们对字符串进 ...
- 675. Cut Off Trees for Golf Event
// Potential improvements: // 1. we can use vector<int> { h, x, y } to replace Element, sortin ...
- python2.7练习小例子(九)
9)1.题目:暂停一秒输出. 程序分析:使用 time 模块的 sleep() 函数. 程序源代码: #!/usr/bin/python # -*- coding: UTF-8 ...
- kafka监听类
package com.datad.dream.service; import com.alibaba.fastjson.JSON; import com.datad.dream.dao.KafkaI ...
- .NET基础知识之八——深拷贝,浅拷贝
目录 1.概念 2.使用赋值符号"=" 3.浅复制 4.深复制 5.问题一:如果类里面嵌套有多个类,然后嵌套类里面又嵌套类,那么像上面实现深拷贝的方法还能用吗? 6.问题二:实现深 ...
- 「暑期训练」「Brute Force」 Bitonix' Patrol (CFR134D1D)
题意 有n" role="presentation">nn个站点,排成圆形,每站间距m" role="presentation"> ...
- 「日常训练」 Soldier and Traveling (CFR304D2E)
题意 (CodeForces 546E) 对一个无向图,给出图的情况与各个节点的人数/目标人数.每个节点的人只可以待在自己的城市或走到与他相邻的节点. 问最后是否有解,输出一可行解(我以为是必须和答案 ...
- Qt 解析网络数据出现ssl错误
最近写了点小东西,哈哈, 网络部分是同学帮我搞的 在编译的时候,出现了一下错误 qt.network.ssl: QSslSocket: cannot call unresolved function ...
- 深挖 NGUI 基础 之UICamera (二)
一.UI Camera作用 UICamera需要挂载在摄像机上才能发挥作用 UICamera仅负责 发送NGUI 事件 到 脚本所附加的摄像机中看得到的对象,比如我自定义了NGUI层(在Inspect ...
- 【java并发编程实战】第七章:取消与关闭
停止线程的几种方式 一般的逻辑停止 public class ThreadInterruptTest { public static volatile boolean cancel = true; p ...