Going Home

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6478    Accepted Submission(s): 3411

Problem Description
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.

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
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
 
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay. 
 
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

题意  n个人 n个房子 在N*M个方格  人移动一格要花费 1(只能水平竖直四个方向)  问n个人走到n个房子的最小花费是多少(一个房子只能一个人待)

解析  这道题可以转化成费用流来解决,n个源点n个汇点 最大流为n的最小费用 ,我们直接建立一个超源点0,一个超汇点n*m+1 然后和源点汇点相连 容量1 费用0

注意 其他边的费用为1 但是容量要设为inf 因为走过之后还可以走.

也可以用二分图最大全匹配写 还没get这项技能。。。

代码一   // 一比二快100ms。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e4+,mod=1e9+,inf=0x3f3f3f3f;
  4. typedef long long ll;
  5. #define pb push_back
  6. #define mp make_pair
  7. #define X first
  8. #define Y second
  9. #define all(a) (a).begin(), (a).end()
  10. #define fillchar(a, x) memset(a, x, sizeof(a))
  11. #define huan printf("\n");
  12. #define debug(a,b) cout<<a<<" "<<b<<" ";
  13. int dir[][]={{,},{-,},{,-},{,}};
  14. char a[maxn][maxn];
  15. struct MCMF {
  16. struct Edge {
  17. int from, to, cap, cost;
  18. Edge(int u, int v, int w, int c): from(u), to(v), cap(w), cost(c) {}
  19. };
  20. int n, s, t;
  21. vector<Edge> edges;
  22. vector<int> G[maxn];
  23. int inq[maxn], d[maxn], p[maxn], a[maxn];
  24.  
  25. void init(int n) {
  26. this->n = n;
  27. for (int i = ; i <= n; i ++) G[i].clear();
  28. edges.clear();
  29. }
  30. void addedge(int from, int to, int cap, int cost) {
  31. edges.push_back(Edge(from, to, cap, cost));
  32. edges.push_back(Edge(to, from, , -cost));
  33. int m = edges.size();
  34. G[from].push_back(m - );
  35. G[to].push_back(m - );
  36. }
  37. bool BellmanFord(int s, int t, int &flow, int &cost) {
  38. for (int i = ; i <= n; i ++) d[i] = inf;
  39. memset(inq, , sizeof(inq));
  40. d[s] = ; inq[s] = ; p[s] = ; a[s] = inf;
  41.  
  42. queue<int> Q;
  43. Q.push(s);
  44. while (!Q.empty()) {
  45. int u = Q.front(); Q.pop();
  46. inq[u] = ;
  47. for (int i = ; i < G[u].size(); i ++) {
  48. Edge &e = edges[G[u][i]];
  49. if (e.cap && d[e.to] > d[u] + e.cost) {
  50. d[e.to] = d[u] + e.cost;
  51. p[e.to] = G[u][i];
  52. a[e.to] = min(a[u], e.cap);
  53. if (!inq[e.to]) {
  54. Q.push(e.to);
  55. inq[e.to] = ;
  56. }
  57. }
  58. }
  59. }
  60. if (d[t] == inf) return false;
  61. flow += a[t];
  62. cost += d[t] * a[t];
  63. int u = t;
  64. while (u != s) {
  65. edges[p[u]].cap -= a[t];
  66. edges[p[u] ^ ].cap += a[t];
  67. u = edges[p[u]].from;
  68. }
  69. return true;
  70. }
  71. int solve(int s, int t) {
  72. int flow = , cost = ;
  73. while (BellmanFord(s, t, flow, cost));
  74. return cost;
  75. }
  76. }solver;;
  77. void build(int n,int m)
  78. {
  79. vector<int> ss,tt;
  80. for(int i=;i<=n;i++)
  81. {
  82. scanf("%s",a[i]+);
  83. }
  84. for(int i=;i<=n;i++)
  85. {
  86. for(int j=;j<=m;j++)
  87. {
  88. int temp=(i-)*m+j;
  89. if(a[i][j]=='m')
  90. ss.push_back(temp);
  91. else if(a[i][j]=='H')
  92. tt.push_back(temp);
  93. for(int k=;k<;k++)
  94. {
  95. int x=i+dir[k][];
  96. int y=j+dir[k][];
  97. if(x>=&&x<=n&&y>=&&y<=m)
  98. {
  99. int temp2=(x-)*m+y;
  100. solver.addedge(temp,temp2,inf,);
  101. // cout<<i<<" "<<j<<" "<<temp<<" "<<temp2<<endl;
  102. }
  103. }
  104. }
  105. }
  106. for(int i=;i<ss.size();i++)
  107. solver.addedge(,ss[i],,);
  108. for(int i=;i<tt.size();i++)
  109. solver.addedge(tt[i],n*m+,,);
  110. }
  111. int main()
  112. {
  113. int n,m;
  114. while(~scanf("%d%d",&n,&m)&&n&&m)
  115. {
  116. solver.init(n*m+);
  117. build(n,m);
  118. int maxflow;
  119. maxflow=solver.solve(,n*m+);
  120. printf("%d\n",maxflow);
  121. }
  122. }

代码二

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<vector>
  6. #include<queue>
  7. using namespace std;
  8. const int maxn=2e4+,mod=1e9+,inf=0x3f3f3f3f;
  9. struct edge
  10. {
  11. int to,next,cap,flow,cost;
  12. } edge[maxn*];
  13. int head[maxn],tol;
  14. int pre[maxn],dis[maxn];
  15. bool vis[maxn];
  16. int N;
  17. char a[maxn][maxn];
  18. void init(int n)
  19. {
  20. N=n,tol=;
  21. memset(head,-,sizeof(head));
  22. }
  23. void addedge(int u,int v,int cap,int cost)
  24. {
  25. edge[tol].to=v;
  26. edge[tol].cap=cap;
  27. edge[tol].flow=;
  28. edge[tol].cost=cost;
  29. edge[tol].next=head[u];
  30. head[u]=tol++;
  31. edge[tol].to=u;
  32. edge[tol].cap=;
  33. edge[tol].flow=;
  34. edge[tol].cost=-cost;
  35. edge[tol].next=head[v];
  36. head[v]=tol++;
  37. }
  38. bool spfa(int s,int t)
  39. {
  40. queue<int> q;
  41. for(int i=; i<=N; i++)
  42. {
  43. dis[i]=inf;
  44. vis[i]=false;
  45. pre[i]=-;
  46. }
  47. dis[s]=;
  48. vis[s]=true;
  49. q.push(s);
  50. while(!q.empty())
  51. {
  52. int u=q.front();
  53. q.pop();
  54. vis[u]=false;
  55. for(int i=head[u]; i!=-; i=edge[i].next)
  56. {
  57. int v=edge[i].to;
  58. if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost)
  59. {
  60. dis[v]=dis[u]+edge[i].cost;
  61. pre[v]=i;
  62. if(!vis[v])
  63. {
  64. vis[v]=true;
  65. q.push(v);
  66. }
  67. }
  68. }
  69. }
  70. if(pre[t]==-) return false;
  71. else return true;
  72. }
  73. int mincostflow(int s,int t,int &cost)
  74. {
  75. int flow=;
  76. cost=;
  77. while(spfa(s,t))
  78. {
  79. int Min=inf;
  80. for(int i=pre[t]; i!=-; i=pre[edge[i^].to])
  81. {
  82. if(Min>edge[i].cap-edge[i].flow)
  83. Min=edge[i].cap-edge[i].flow;
  84. }
  85. for(int i=pre[t]; i!=-; i=pre[edge[i^].to])
  86. {
  87. edge[i].flow+=Min;
  88. edge[i^].flow-=Min;
  89. cost+=edge[i].cost*Min;
  90. }
  91. flow+=Min;
  92. }
  93. return flow;
  94. }
  95. int dir[][]={{,},{-,},{,-},{,}};
  96. void build(int n,int m)
  97. {
  98. vector<int> ss,tt;
  99. for(int i=;i<=n;i++)
  100. {
  101. scanf("%s",a[i]+);
  102. }
  103. for(int i=;i<=n;i++)
  104. {
  105. for(int j=;j<=m;j++)
  106. {
  107. int temp=(i-)*m+j;
  108. if(a[i][j]=='m')
  109. ss.push_back(temp);
  110. else if(a[i][j]=='H')
  111. tt.push_back(temp);
  112. for(int k=;k<;k++)
  113. {
  114. int x=i+dir[k][];
  115. int y=j+dir[k][];
  116. if(x>=&&x<=n&&y>=&&y<=m)
  117. {
  118. int temp2=(x-)*m+y;
  119. addedge(temp,temp2,inf,);
  120. // cout<<i<<" "<<j<<" "<<temp<<" "<<temp2<<endl;
  121. }
  122. }
  123. }
  124. }
  125. for(int i=;i<ss.size();i++)
  126. addedge(,ss[i],,);
  127. for(int i=;i<tt.size();i++)
  128. addedge(tt[i],n*m+,,);
  129. }
  130. int main()
  131. {
  132. int n,m;
  133. while(~scanf("%d%d",&n,&m)&&n&&m)
  134. {
  135. init(n*m+);
  136. build(n,m);
  137. int ans,maxflow;
  138. maxflow=mincostflow(,n*m+,ans);
  139. printf("%d\n",ans);
  140. }
  141. }

HDU1533 最小费用最大流的更多相关文章

  1. hdu1533 Going Home 最小费用最大流 构造源点和汇点

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

  2. [hdu1533]二分图最大权匹配 || 最小费用最大流

    题意:给一个n*m的地图,'m'表示人,'H'表示房子,求所有人都回到房子所走的距离之和的最小值(距离为曼哈顿距离). 思路:比较明显的二分图最大权匹配模型,将每个人向房子连一条边,边权为曼哈顿距离的 ...

  3. [板子]最小费用最大流(Dijkstra增广)

    最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...

  4. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  5. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  8. 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 821  Solved: 502[Submit][Status ...

  9. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

随机推荐

  1. 最优雅退出 Android 应用程序的 6 种方式

    一.容器式 建立一个全局容器,把所有的Activity存储起来,退出时循环遍历finish所有Activity import java.util.ArrayList; import java.util ...

  2. Angular JS中变量定义的基本原则

    在Angular JS开发中,经常需要定义一些变量,关于这些变量的定义方法及作用域应该注意以下几点: 1. 如果能用局部变量解决问题,尽量不要用全局变量. 2. 需要与界面双向绑定的变量采用$scop ...

  3. monkeyrunner 简单用例编写

    monkeyrunnerfrom com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImagedevice = Monke ...

  4. 51nod 1432 独木舟

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承 ...

  5. 千万千万不要运行的 Linux 命令

    文中列出的命令绝对不可以运行,即使你觉得很好奇也不行,除非你是在虚拟机上运行(出现问题你可以还原),因为它们会实实在在的破坏你的系统.所以不在root等高级管理权限下执行命令是很好的习惯. 早晚有一天 ...

  6. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'needDao' defined in URL

    这个是我修改过后的mapper,是我的mapper中的空间地址写错了呢

  7. 关于dzzoffice 破解版

    最近看到很多人在搜索dzzoffice破解版,其实dzzoffie是一款全开源的产品,开放的功能是与演示站中一摸一样的,所以并不会有人破解这种全开源的系统.那么为什么会有人搜索这样的关键词呢? 可能大 ...

  8. Linux php安装zip扩展

    Linux php安装zip扩展 2018.07.22 22:40 1165浏览   #wget http://pecl.php.net/get/zip-1.12.4.tgz #tar zxfv zi ...

  9. DROP FUNCTION - 删除一个函数

    SYNOPSIS DROP FUNCTION name ( [ type [, ...] ] ) [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP FUNCTION ...

  10. chrome 打开上次关闭的tab ctrl+shift+T

    chrome 打开上次关闭的tab ctrl+shift+T