题目链接:https://vjudge.net/problem/POJ-2195

思路:曼哈顿距离来求每个人到每个房间的距离,把距离当作费用。

就可以用最小费用最大流来解决了,把每个房子拆成两个点,限流。

源点->人->房入->房出->汇点。流量的话都设置为1,起到限流作用。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std; const int N = ,INF = (int)1e9;
int n,m,tot,num;
int head[N<<],d[N<<],vis[N<<],pre[N<<];
char mp[N][N];
vector<pair<int,int > > p;
vector<pair<int,int > > h;
struct node{
int to,nxt,cap,flow,cost;
}e[N*N]; void show(){
cout << endl;
for(int i = ; i < n; ++i) cout << mp[i] << endl;
for(int i = ; i < num; ++i) printf("( %d, %d) ",p[i].first,p[i].second);
cout << endl;
for(int i = ; i < num; ++i) printf("( %d, %d) ",h[i].first,h[i].second);
cout << endl << endl;
}
//求距离
inline int _dis(int x,int y){
return abs(p[x].first - h[y].first) + abs(p[x].second - h[y].second);
} inline void add(int u,int v,int cap,int flow,int cost){
e[tot].to = v;
e[tot].cap = cap;
e[tot].flow = flow;
e[tot].cost = cost;
e[tot].nxt = head[u];
head[u] = tot++;
e[tot].to = u;
e[tot].cap = ;
e[tot].flow = flow;
e[tot].cost = -cost;
e[tot].nxt = head[v];
head[v] = tot++;
} void build_map(int s,int t){ //0源点 1~num人 num+1~2*num 房入 2*num+1~3*num房出 3*num+1汇点
int cost;
for(int i = ; i < num; ++i){
for(int j = ; j < num; ++j){
cost = _dis(i,j);
add(i+,j++num,,,cost);
}
}
for(int i = ; i < num; ++i) add(s,i+,,,);
for(int i = ; i < num; ++i) add(i++num,i++*num,,,);
for(int i = ; i < num; ++i) add(i++*num,t,,,);
} bool spfa(int s,int t){
for(int i = s; i <= t; ++i) pre[i] = -;
for(int i = s; i <= t; ++i) d[i] = INF; d[s] = ;
for(int i = s; i <= t; ++i) vis[i] = false; vis[s] = true;
queue<int > que;
que.push(s);
while(!que.empty()){
int now = que.front(); que.pop();
vis[now] = false;
for(int o = head[now]; ~o; o = e[o].nxt){
int to = e[o].to;
if(e[o].cap > e[o].flow && d[to] > d[now] + e[o].cost){
d[to] = d[now] + e[o].cost;
pre[to] = o;
if(!vis[to])
vis[to] = true;
que.push(to);
}
}
}
if(pre[t] == -) return false;
else return true;
} int work(){ int s = ,t = *num+,ans = ;
for(int i = s; i <= t; ++i) head[i] = -; tot = ;
build_map(s,t);
while(spfa(s,t)){
int Min = INF;
for(int o = pre[t]; ~o; o = pre[e[o^].to]){
Min = min(Min,e[o].cap - e[o].flow);
}
for(int o = pre[t]; ~o; o = pre[e[o^].to]){
e[o].flow += Min;
e[o^].flow -= Min;
}
ans += Min*d[t];
}
return ans;
} int main(){ while(~scanf("%d%d",&n,&m) && (n+m)){
for(int i = ; i < n; ++i) scanf("%s",mp[i]);
p.clear(); h.clear();
for(int i = ; i < n; ++i){
for(int j = ; j < m; ++j){
if(mp[i][j] == 'm') p.push_back(make_pair(i,j));
if(mp[i][j] == 'H') h.push_back(make_pair(i,j));
}
}
num = p.size();
//show();
//cout << "------------------------------------" << work() << endl;
cout << work() << endl;
} return ;
}

kuangbin专题专题十一 网络流 Going Home POJ - 2195的更多相关文章

  1. kuangbin专题专题十一 网络流 Minimum Cost POJ - 2516

    题目链接:https://vjudge.net/problem/POJ-2516 思路:对于每种商品跑最小费用最大流,如果所有商品和人一起建图跑,O(v^2*m)数量级太大,会超时. 把店里的商品拆点 ...

  2. 图论--网络流--费用流POJ 2195 Going Home

    Description On a grid map there are n little men and n houses. In each unit time, every little man c ...

  3. [kuangbin带你飞]专题十一 网络流

            ID Origin Title   34 / 81 Problem A POJ 3436 ACM Computer Factory   92 / 195 Problem B POJ 3 ...

  4. Kuangbin 带你飞专题十一 网络流题解 及模版 及上下界网络流等问题

    首先是几份模版 最大流:虽然EK很慢但是优势就是短.求最小割的时候可以根据增广时的a数组来判断哪些边是割边.然而SAP的最大流版我只会套版,并不知道该如何找到这个割边.在尝试的时候发现了一些问题.所以 ...

  5. POJ 2195 Going Home 最小费用最大流 尼玛,心累

    D - Going Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  6. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  7. POJ 2195 Going Home / HDU 1533(最小费用最大流模板)

    题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...

  8. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

  9. poj 2195 Going Home(最小费最大流)

    poj 2195 Going Home Description On a grid map there are n little men and n houses. In each unit time ...

随机推荐

  1. Linux 命令整理 vim

    Vim  一.官方网站 http://www.vim.org 二.背景 所有的 Unix Like 系统都会内建 vi 文书编辑器,但是在我们编程这里开发使用最多的要数 vim命令了. 三.操作 三种 ...

  2. 彻底搞懂HTML5文件上传操作需要的相关资料

    https://developer.mozilla.org/zh-CN/docs/Web/GuideMDN Web Guide https://developer.mozilla.org/zh-CN/ ...

  3. 第二阶段:2.商业需求分析及BRD:1.产品需求管理

    产品经理需要投入大量的时间在需求方面的工作. 一张图看出需求多么重要.各个方面的人物对需求的误解导致的后果. 首先收集需求 需求采集 可以用excel收集数据并整理 备注信息也很重要 有时候要跟提出人 ...

  4. 使用spring boot中的JPA操作数据库

    前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...

  5. Google被墙怎么办?

    Google被墙怎么办? 1 声明 请小伙伴们遵守法律法规,我们只是为了更好的查询学习资料. 想使用Google查询相关资料 想使用Google账号管理收藏夹 想使用Google商店安装软件 == 2 ...

  6. linux Nginx-1.10.2 安装部署教程

    一.下载Nginx以及依赖组件[root@localhost src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz [root@localh ...

  7. 程序员初学者参考 ---懂得基础语法后如何做一个自己的case?

    对于很多人来说,我懂java语法,甚至面向对象的特性啦这些都是有了解的,但我就是不会做项目,其实项目真有那么难吗? 对于基础不牢固的人来说,我还不会这个基础点,那个还没学呢,你让我做个项目,我保证做不 ...

  8. 1060 爱丁顿数 (25 分)C语言

    英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个"爱丁顿数" E ,即满足有 E 天骑车超过 E 英里的最大整数 E.据说爱丁顿自己的 E 等于87. 现给 ...

  9. (四)注册登录--重用Django

    一.使用已有登录功能 (1)进入Lib\sitepackages\django\contrib\admin\templates\registration下,将对应模板,复制到项目template模板中 ...

  10. KMP 和 扩展KMP

    KMP:在主串S中找子串T的位置KMP算法的时间复杂度O(|S|+|T|). #define maxn 1000 char s[maxn],t[maxn];//s为主串,t为子串 int net[ma ...