题目大意:

有一个最大是100 * 100 的网格图,上面有 s 个 房子和人,人每移动一个格子花费1的代价,求最小代价让所有的人都进入一个房子。每个房子只能进入一个人。

算法讨论:

注意是KM 和 MCMF算法,我写的是MCMF算法,一开始想的是连10000个点,但是不会连那些大众点之间的边,只会连超级点和普通点之间的边。后来觉得只要连房子点和

人点就可以了。连从人到房子的边,容量是1,花费是他们之间的曼哈顿距离,然后超级源点和超级汇点像上面那样连接,注意连点的时候把他们每个点都具体化一下,就是把点值

都精确到一个连续的范围内去。然后做从超级源点到超级汇点的MCMF算法就可以了。至于那10000个之间的连边,觉得虽然效率不高,但是还是有必要考虑一下。大家有知道的撒

告诉一下。感谢万分。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std; struct MCMF{
static const int N = * + ;
static const int M = * * + ;
static const int oo = 0x3f3f3f3f; int n, m, s, t, tot;
int first[N], next[M];
int u[M], v[M], cap[M], flow[M], cost[M];
int dis[N], a[N], inque[N], pre[N]; void Clear(){memset(first, -, sizeof first);tot = ;} void Add(int from, int to, int cp, int flw, int ct){
u[tot] = from; v[tot] = to; cap[tot] = cp; flow[tot] = ; cost[tot] = ct;
next[tot] = first[u[tot]];
first[u[tot]] = tot; ++ tot;
u[tot] = to; v[tot] = from; cap[tot] = ; flow[tot] = ; cost[tot] = -ct;
next[tot] = first[u[tot]];
first[u[tot]] = tot; ++ tot;
} bool bfs(int &flw, int &ct){
for(int i = ; i <= n + ; ++ i) dis[i] = oo;
memset(inque, , sizeof inque);
dis[s] = ; pre[s] = ; a[s] = oo; inque[s] = ; queue <int> q;
q.push(s);
while(!q.empty()){
int now = q.front(); q.pop();
inque[now] = ; for(int i = first[now]; i != -; i = next[i]){
if(cap[i] > flow[i] && dis[v[i]] > dis[now] + cost[i]){
dis[v[i]] = dis[now] + cost[i];
a[v[i]] = min(a[now], cap[i] - flow[i]);
pre[v[i]] = i;
if(!inque[v[i]]){
inque[v[i]] = ; q.push(v[i]);
}
}
}
} if(dis[t] == oo) return false;
flw += a[t];
ct += dis[t] * a[t]; int now = t;
while(now != s){
flow[pre[now]] += a[t];
flow[pre[now]^] -= a[t];
now = u[pre[now]];
}
return true;
} int MinCostMaxFlow(int s, int t){
this->s = s;this->t = t;
int flw = , ct = ;
while(bfs(flw, ct));
return ct;
}
}Net; struct Position{
int l, r, id;
Position(int _l=, int _r=, int _id=): l(_l), r(_r), id(_id){}
}mm[], HH[]; int ns, ms, cnt1, cnt2, tp1, tp2;
char str[][]; void Solve(){
for(int i = ; i <= tp1; ++ i){
for(int j = ; j <= tp2; ++ j){
int x1 = mm[i].l, y1 = mm[i].r;
int x2 = HH[j].l, y2 = HH[j].r;
Net.Add(mm[i].id, HH[j].id, , , abs(x1-x2) + abs(y1-y2));
}
}
} int main(){ while(scanf("%d%d", &ns, &ms) && ns && ms){
Net.Clear();
cnt1 = cnt2 = ;
tp1 = tp2 = ;
for(int i = ; i <= ns; ++ i)
scanf("%s", str[i] + );
for(int i = ; i <= ns; ++ i){
for(int j = ; j <= ms; ++ j){
if(str[i][j] == 'm') ++ tp1;
else if (str[i][j] == 'H') ++ tp2;
}
}
Net.n = tp1 + tp2;
for(int i = ; i <= ns; ++ i){
for(int j = ; j <= ms; ++ j){
if(str[i][j] == 'm'){
++ cnt1;
Net.Add(, cnt1, , , );
mm[cnt1] = (Position){i, j, cnt1};
}
}
}
for(int i = ; i <= ns; ++ i){
for(int j = ; j <= ms; ++ j){
if(str[i][j] == 'H'){
++ cnt2;
Net.Add(tp1 + cnt2, Net.n + , , , );
HH[cnt2] = (Position){i, j, tp1 + cnt2};
}
}
}
Solve();
printf("%d\n", Net.MinCostMaxFlow(, Net.n + ));
} return ;
}

POJ 2195/HDU 1533

POJ 2195 Going Home / HDU 1533(最小费用最大流模板)的更多相关文章

  1. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

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

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

  3. hdu 1533(最小费用最大流)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  5. HDU 1533 最小费用最大流(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...

  6. HDU3376 最小费用最大流 模板2

    Matrix Again Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)To ...

  7. 图论算法-最小费用最大流模板【EK;Dinic】

    图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...

  8. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

  9. 最大流 && 最小费用最大流模板

    模板从  这里   搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...

  10. poj 2195 最小费用最大流模板

    /*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...

随机推荐

  1. 关于Ajax的技术组成与核心原理

    1.Ajax 特点: 局部刷新.提高用户的体验度,数据从服务器商加载 2.AJax的技术组成 不是新技术,而是之前技术的整合 Ajax: Asynchronous Javascript And Xml ...

  2. shell中的case表达式

    语法格式 case var in pattern1 | patter2) command1 command2;; pattern3) command1 command2;; *) default co ...

  3. HDU 4620 Fruit Ninja Extreme(2013多校第二场 剪枝搜索)

    这题官方结题报告一直在强调不难,只要注意剪枝就行. 这题剪枝就是生命....没有最优化剪枝就跪了:如果当前连续切割数加上剩余的所有切割数没有现存的最优解多的话,不需要继续搜索了 #include &l ...

  4. JavaScript语法学习笔记

    1.关于执行JavaScript代码的方法: 第一种方法是将JavaScript代码放到文档<head>标签中的<script>标签之间: <head>     & ...

  5. Remove掉Request.QueryString

    好久上博客来了,最近有点忙,有点懒. 今天在解决一个Request.QueryString 传值的问题上遇到了,当不是第一次加载时需要把Request.QueryString的值赋值为null,刚开始 ...

  6. CSS常见BUG

    CSS Hack IE条件注释: 所有IE:<!--[if IE]> css code <![endif]--> IE6以上:<!--[if gt IE 6]> c ...

  7. HDU 5492(DP) Find a path

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...

  8. 关于hash

    http://rapheal.iteye.com/blog/1142955 关于javascript hash

  9. NOR flash和NAND flash区别,RAM 和ROM区别d

    ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是Random Access Memory的缩写.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都是 ...

  10. 【转】 Ubuntu下配置USB转串口及串口工具配置--不错

    原文网址:http://blog.csdn.net/dreambegin/article/details/6985028 注意:默认情况下ubuntu已经安装了USB转串口驱动(pl2303).我的是 ...