题目大意:

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

算法讨论:

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

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

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

告诉一下。感谢万分。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. struct MCMF{
  11. static const int N = * + ;
  12. static const int M = * * + ;
  13. static const int oo = 0x3f3f3f3f;
  14.  
  15. int n, m, s, t, tot;
  16. int first[N], next[M];
  17. int u[M], v[M], cap[M], flow[M], cost[M];
  18. int dis[N], a[N], inque[N], pre[N];
  19.  
  20. void Clear(){memset(first, -, sizeof first);tot = ;}
  21.  
  22. void Add(int from, int to, int cp, int flw, int ct){
  23. u[tot] = from; v[tot] = to; cap[tot] = cp; flow[tot] = ; cost[tot] = ct;
  24. next[tot] = first[u[tot]];
  25. first[u[tot]] = tot; ++ tot;
  26. u[tot] = to; v[tot] = from; cap[tot] = ; flow[tot] = ; cost[tot] = -ct;
  27. next[tot] = first[u[tot]];
  28. first[u[tot]] = tot; ++ tot;
  29. }
  30.  
  31. bool bfs(int &flw, int &ct){
  32. for(int i = ; i <= n + ; ++ i) dis[i] = oo;
  33. memset(inque, , sizeof inque);
  34. dis[s] = ; pre[s] = ; a[s] = oo; inque[s] = ;
  35.  
  36. queue <int> q;
  37. q.push(s);
  38. while(!q.empty()){
  39. int now = q.front(); q.pop();
  40. inque[now] = ;
  41.  
  42. for(int i = first[now]; i != -; i = next[i]){
  43. if(cap[i] > flow[i] && dis[v[i]] > dis[now] + cost[i]){
  44. dis[v[i]] = dis[now] + cost[i];
  45. a[v[i]] = min(a[now], cap[i] - flow[i]);
  46. pre[v[i]] = i;
  47. if(!inque[v[i]]){
  48. inque[v[i]] = ; q.push(v[i]);
  49. }
  50. }
  51. }
  52. }
  53.  
  54. if(dis[t] == oo) return false;
  55. flw += a[t];
  56. ct += dis[t] * a[t];
  57.  
  58. int now = t;
  59. while(now != s){
  60. flow[pre[now]] += a[t];
  61. flow[pre[now]^] -= a[t];
  62. now = u[pre[now]];
  63. }
  64. return true;
  65. }
  66.  
  67. int MinCostMaxFlow(int s, int t){
  68. this->s = s;this->t = t;
  69. int flw = , ct = ;
  70. while(bfs(flw, ct));
  71. return ct;
  72. }
  73. }Net;
  74.  
  75. struct Position{
  76. int l, r, id;
  77. Position(int _l=, int _r=, int _id=): l(_l), r(_r), id(_id){}
  78. }mm[], HH[];
  79.  
  80. int ns, ms, cnt1, cnt2, tp1, tp2;
  81. char str[][];
  82.  
  83. void Solve(){
  84. for(int i = ; i <= tp1; ++ i){
  85. for(int j = ; j <= tp2; ++ j){
  86. int x1 = mm[i].l, y1 = mm[i].r;
  87. int x2 = HH[j].l, y2 = HH[j].r;
  88. Net.Add(mm[i].id, HH[j].id, , , abs(x1-x2) + abs(y1-y2));
  89. }
  90. }
  91. }
  92.  
  93. int main(){
  94.  
  95. while(scanf("%d%d", &ns, &ms) && ns && ms){
  96. Net.Clear();
  97. cnt1 = cnt2 = ;
  98. tp1 = tp2 = ;
  99. for(int i = ; i <= ns; ++ i)
  100. scanf("%s", str[i] + );
  101. for(int i = ; i <= ns; ++ i){
  102. for(int j = ; j <= ms; ++ j){
  103. if(str[i][j] == 'm') ++ tp1;
  104. else if (str[i][j] == 'H') ++ tp2;
  105. }
  106. }
  107. Net.n = tp1 + tp2;
  108. for(int i = ; i <= ns; ++ i){
  109. for(int j = ; j <= ms; ++ j){
  110. if(str[i][j] == 'm'){
  111. ++ cnt1;
  112. Net.Add(, cnt1, , , );
  113. mm[cnt1] = (Position){i, j, cnt1};
  114. }
  115. }
  116. }
  117. for(int i = ; i <= ns; ++ i){
  118. for(int j = ; j <= ms; ++ j){
  119. if(str[i][j] == 'H'){
  120. ++ cnt2;
  121. Net.Add(tp1 + cnt2, Net.n + , , , );
  122. HH[cnt2] = (Position){i, j, tp1 + cnt2};
  123. }
  124. }
  125. }
  126. Solve();
  127. printf("%d\n", Net.MinCostMaxFlow(, Net.n + ));
  128. }
  129.  
  130. return ;
  131. }

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. table超过30个字段如何处理呢? bootstrap

    样式: @media (max-width: 768px) { .table-supplier { width: 100%; height: 100%; margin-bottom: 12.75px; ...

  2. CI框架uri去掉index.php

    CI框架的入口是index.php,所以url实际上要多出一个index.php,非常不美观.我使用的是apache服务器,要开启mod_rewrite服务才可以. sudo a2enmod rewr ...

  3. AIX下解决POWERHA的脑裂问题

    一.安装创建并发vg时必需的软件包clvm包,该包安装.升级.后必须重启os clvm包的描述:Enhanced Concurrent Logical Volume Manager 软件包在aix61 ...

  4. JS函数自动执行

    关于让网页中的JavaScript函数自动执行,方法就多洛,但是万变不离其宗,下面给大家介绍一下! 前提条件,网页中必须有JS函数代码,或者,使用文件导入的方法也行: 在HTML中的Head区域中,有 ...

  5. Codeforces Round #277 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...

  6. java中连接postgresql基本代码

    try { Class.forName( "org.postgresql.Driver" ).newInstance(); String url = "jdbc:post ...

  7. BZOJ 1017 魔兽地图DotR(树形DP)

    题意:有两类装备,高级装备A和基础装备B.现在有m的钱.每种B有一个单价和可以购买的数量上限.每个Ai可以由Ci种其他物品合成,给出Ci种其他物品每种需要的数量.每个装备有一个贡献值.求最大的贡献值. ...

  8. 《Programming WPF》翻译 第8章 1.动画基础

    原文:<Programming WPF>翻译 第8章 1.动画基础 动画包括在一段时间内改变用户界面的某些可见的特征,如它的大小.位置或颜色.你可以做到这一点,非常困难的通过创建一个tim ...

  9. sae-v2ex 一个运行在SAE上的类似v2ex的轻型python论坛 - 技术讨论 - 云计算开发者社区 - Powered by Discuz!

    sae-v2ex 一个运行在SAE上的类似v2ex的轻型python论坛 - 技术讨论 - 云计算开发者社区 - Powered by Discuz! sae-v2ex 一个运行在SAE上的类似v2e ...

  10. <转载>linux gcc编译器中使用gdb单步调试程序,程序不是顺序执行的。

    原文地址http://blog.csdn.net/abc78400123/article/details/6779108 在用gdb调试,使用s 或n单步执行程序时,发现程序不是按顺序运行的,有时莫名 ...