POJ 2195 Going Home 最小费用流 难度:1
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17955 | Accepted: 9145 |
Description
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
Output
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
扫描路径得到人和房子的坐标,相减得所费路程,然后建边最小费用流即可
#include<cstdio>
#include <queue>
#include <algorithm>
#include <assert.h>
#include <cstring>
using namespace std;
const int inf=0x7fffffff;
int n,m; char maz[][];
int man[][];
int house[][];
int hlen,mlen; const int sups=;
const int supt=; int cost[][];
int f[][];
int e[][];
int len[]; int d[];
int pre[];
bool vis[]; queue<int >que; int main(){
while(scanf("%d%d",&n,&m)==&&n&&m){
input:
hlen=mlen=;
gets(maz[]);
for(int i=;i<n;i++){
gets(maz[i]);
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(maz[i][j]=='m'){
man[mlen][]=i;man[mlen++][]=j;
}
else if(maz[i][j]=='H'){
house[hlen][]=i;house[hlen++][]=j;
}
}
} memset(f,,sizeof(f));
memset(cost,,sizeof(cost));
fill(len,len+mlen,hlen+);fill(len+mlen,len+mlen+hlen,mlen+);len[sups]=mlen;len[supt]=hlen;
bulidedge:
for(int i=;i<mlen;i++){
f[sups][i]=;
e[sups][i]=i;
e[i][]=sups;
for(int j=;j<hlen;j++){
e[i][j+]=j+mlen;
e[j+mlen][i+]=i;
f[i][j+mlen]=;
cost[i][j+mlen]=abs(man[i][]-house[j][])+abs(man[i][]-house[j][]);
cost[j+mlen][i]=-cost[i][j+mlen];
}
}
for(int j=;j<hlen;j++){
e[supt][j]=j+mlen;
e[j+mlen][]=supt;
f[j+mlen][supt]=;
} mincostflow:
int ans=;
int flow=mlen;
while(flow>){
fill(d,d+,inf);
d[sups]=;
que.push(sups);
while(!que.empty()){
int fr=que.front();que.pop();
vis[fr]=false;
for(int i=;i<len[fr];i++){
int to=e[fr][i];
if(f[fr][to]>&&d[to]>d[fr]+cost[fr][to]){
d[to]=d[fr]+cost[fr][to];
pre[to]=fr;
if(!vis[to]){
que.push(to);
vis[to]=true;
}
}
}
} assert(d[supt]!=inf); int sub=flow;
for(int i=supt;i!=sups;i=pre[i]){
sub=min(flow,f[pre[i]][i]);
}
flow-=sub;
ans+=sub*d[supt];
for(int i=supt;i!=sups;i=pre[i]){
f[pre[i]][i]-=sub;
f[i][pre[i]]+=sub;
} }
printf("%d\n",ans);
}
return ;
}
POJ 2195 Going Home 最小费用流 难度:1的更多相关文章
- POJ 2195 Going Home 最小费用流 裸题
给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...
- POJ 2195 Going Home 最小费用流
POJ2195 裸的最小费用流,当然也可以用KM算法解决,但是比较难写. 注意反向边的距离为正向边的相反数(因此要用SPFA) #include<iostream> #include< ...
- POJ 2516 Minimum Cost 最小费用流 难度:1
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 13511 Accepted: 4628 Des ...
- 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 ...
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
随机推荐
- java在访问https资源时的证书信任问题
java程序在访问https资源时,出现报错 sun.security.validator.ValidatorException: PKIX path building failed: sun.sec ...
- C#调用非托管dll
以C#开发周立功CAN举例,在官网下载了周立功的demo 一.C++头文件样子 //接口卡类型定义#define VCI_PCI5121 1 //一些结构体定义 typedef struct tagR ...
- cmd解释
cmd是command的缩写.即命令提示符(CMD),是在OS / 2 , Windows CE与Windows NT平台为基础的操作系统(包括Windows 2000和XP中, Vista中,和Se ...
- 【前端】javascript+jquery实现手风琴式的滚动banner或产品展示图
实现效果 实现步骤 // 鼠标放入到li中该盒子变宽,其他盒子变窄,鼠标移开大盒子,恢复原样 // 实现步骤 // 1. 给li添加背景 // 2. 绑定onmouseover事件,鼠标放入到li中, ...
- 最大子段和SP1716GSS3 线段树
前言 spoj需要FQ注册,比较麻烦,大家就在luogu评测吧 题目大意: $n$ 个数,$q$ 次操作 操作$0 _ x_ y$把$A_x$ 修改为$y$ 操作$1 _ l _r$询问区间$[l, ...
- Centos7.2 安装Elasticsearch 6
下载 elasticsearch.6.0.0.tar.gz 迁移文件到usr/local中 mv elasticsearch-.tar.gz /usr/local/ cd /usr/local tar ...
- 切面条|2014年蓝桥杯B组题解析第二题-fishers
切面条 一根高筋拉面,中间切一刀,可以得到2根面条. 如果先对折1次,中间切一刀,可以得到3根面条. 如果连续对折2次,中间切一刀,可以得到5根面条. 那么,连续对折10次,中间切一刀,会得到多少面条 ...
- ZOJ 2083 Win the Game(SG函数)题解
题意:给一端n块的板,两人玩,每次能涂相邻两块没涂过的板,不能涂的人为输,先手赢输出yes 思路:sg函数打表,练习题 代码: #include<queue> #include<cs ...
- 小Z的袜子(莫队分块)题解
小Z的袜子(hose) 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- 【第三十六章】 metrics(4)- metrics-graphite
将metrics report给graphite(carbon-relay) 一.代码 1.pom.xml <!-- metrics-graphite --> <dependency ...