kuangbin专题专题十一 网络流 Going Home POJ - 2195
题目链接: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的更多相关文章
- kuangbin专题专题十一 网络流 Minimum Cost POJ - 2516
题目链接:https://vjudge.net/problem/POJ-2516 思路:对于每种商品跑最小费用最大流,如果所有商品和人一起建图跑,O(v^2*m)数量级太大,会超时. 把店里的商品拆点 ...
- 图论--网络流--费用流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 ...
- [kuangbin带你飞]专题十一 网络流
ID Origin Title 34 / 81 Problem A POJ 3436 ACM Computer Factory 92 / 195 Problem B POJ 3 ...
- Kuangbin 带你飞专题十一 网络流题解 及模版 及上下界网络流等问题
首先是几份模版 最大流:虽然EK很慢但是优势就是短.求最小割的时候可以根据增广时的a数组来判断哪些边是割边.然而SAP的最大流版我只会套版,并不知道该如何找到这个割边.在尝试的时候发现了一些问题.所以 ...
- POJ 2195 Going Home 最小费用最大流 尼玛,心累
D - Going Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
题目大意: 有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子.每个房子只能进入一个人. 算法讨论: 注意是KM 和 ...
- POJ 2195 Going Home (带权二分图匹配)
POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...
- 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 ...
随机推荐
- Android TextView调用Settext()耗时的原因
当textview的宽设置为wrap_content的时候,底层会调用checkForRelayout函数,这个函数根据文字的多少重新开始布局 因此将宽度设置为固定值或者match_parent的时候 ...
- 29(30).socket网络基础
转载:https://www.cnblogs.com/linhaifeng/articles/6129246.html 一 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构 互联网 ...
- Omnigraffle 许可证
名字:Appked 序列号:MFWG-GHEB-HYTW-CGHT-CSXU-QCNC-SXU
- Selenium3+Python3环境搭建
安装python 官网直接下载当前最新版的python Python官网地址:https://www.python.org/ 百度搜索Python3.6安装步骤 Python安装步骤:自行百度,暂不提 ...
- TCP/IP|| 建立连接或终止
1.TCP是一个面向连接的协议,在双方发送数据时需要之间建立连接. 当使用telnet命令是连接对应的端口产生TCP连接,通过tcpdump命令查看TCP报文段的输出 源>目的:标志 在标识中有 ...
- AutoCad 二次开发 Jig操作之墙块的拖动
测试结果: 主要思路:选择一段多段线,使用封装的jig类进行实时拖动,其原理就是在拖动的时候,确定被拖动的边,我是选择离输入第一个点最近的边作为拖动边,有了这条边,就能确定需要实时更改的点了,然后当鼠 ...
- 菜鸟学习Fabric源码学习 — 背书节点和链码容器交互
Fabric 1.4 源码分析 背书节点和链码容器交互 本文档主要介绍背书节点和链码容器交互流程,在Endorser背书节点章节中,无论是deploy.upgrade或者调用链码,最后都会调用Chai ...
- 面试时面试官想要听到什么答案(关于一些vue的问题)
前言 自己也只是一个前端的小白,因为公司大佬都比较忙,面试这种事就落到了我这小白身上,第一次叫我去的时候我是百般拒绝的,因为自己还是有自知之明的,但是别人实在抽不开身,没办法只能去了,他们开玩笑说就按 ...
- Netty 的基本简单实例【服务端-客户端通信】
Netty是建立在NIO基础之上,Netty在NIO之上又提供了更高层次的抽象. 在Netty里面,Accept连接可以使用单独的线程池去处理,读写操作又是另外的线程池来处理. Accept连接和读写 ...
- 快速掌握—HTML快速实现自定义Input开关
HTML <input id="customSwitch" type="checkbox" /> <label for="custo ...