1.  

Going Home

  1. Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5504    Accepted Submission(s): 2890
Problem Description
  1.  
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.

  1.  
 
  1.  
Input
  1.  
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.
  1.  
 
  1.  
Output
  1.  
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
  1.  
 
  1.  
Sample Input
  1.  
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
  1.  
 
  1.  
Sample Output
  1.  
2
10
28
  1.  
 
  1.  
Source
  1.  
  1. /**
  2. 题目:hdu1533 Going Home
  3. 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533
  4. 题意:n个人回到n个房子,每个房子只能住一个人。求最少花费。
  5. 思路:
  6. 构造一个源点,到达所有的人,cap = 1, cost = 0;
  7. 构造一个汇点,所有的房子到汇点,cap = 1, cost = 0;
  8. 所有的人到所有的房子,cap = 1, cost = 人到房子的最短距离。
  9. 然后最小费用最大流算法。
  10. */
  11. #include<iostream>
  12. #include<cstring>
  13. #include<vector>
  14. #include<cstdio>
  15. #include<algorithm>
  16. #include<queue>
  17. using namespace std;
  18. const int INF = 0x3f3f3f3f;
  19. typedef long long LL;
  20. const int N = ;
  21. struct Edge{
  22. int from, to, cap, flow, cost;
  23. Edge(int u,int v,int c,int f,int cost):from(u),to(v),cap(c),flow(f),cost(cost){}
  24. };
  25. struct MCMF
  26. {
  27. int n, m, s, t;
  28. vector<Edge> edges;
  29. vector<int> G[N];
  30. int inq[N];
  31. int d[N];
  32. int p[N];
  33. int a[N];
  34.  
  35. void init(int n)
  36. {
  37. this->n = n;
  38. for(int i = ; i <= n; i++) G[i].clear();
  39. edges.clear();
  40. }
  41. void AddEdge(int from,int to,int cap,int cost){
  42. edges.push_back((Edge){from,to,cap,,cost});
  43. edges.push_back((Edge){to,from,,,-cost});
  44. m = edges.size();
  45. G[from].push_back(m-);
  46. G[to].push_back(m-);
  47. }
  48.  
  49. bool BellmanFord(int s,int t,int &flow,int &cost){
  50. for(int i = ; i <= n; i++) d[i] = INF;
  51. memset(inq, , sizeof inq);
  52. d[s] = ; inq[s] = ; p[s] = ; a[s] = INF;
  53.  
  54. queue<int> Q;
  55. Q.push(s);
  56. while(!Q.empty()){
  57. int u = Q.front(); Q.pop();
  58. inq[u] = ;
  59. for(int i = ; i < G[u].size(); i++){
  60. Edge& e = edges[G[u][i]];
  61. if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
  62. d[e.to] = d[u]+e.cost;
  63. p[e.to] = G[u][i];
  64. a[e.to] = min(a[u],e.cap-e.flow);
  65. if(!inq[e.to]){ Q.push(e.to); inq[e.to] = ;}
  66. }
  67. }
  68. }
  69. if(d[t]==INF) return false;
  70. flow += a[t];
  71. cost += d[t]*a[t];
  72. int u = t;
  73. while(u!=s){
  74. edges[p[u]].flow += a[t];
  75. edges[p[u]^].flow -= a[t];
  76. u = edges[p[u]].from;
  77. }
  78. return true;
  79. }
  80. int Mincost(int s,int t)
  81. {
  82. int flow = , cost = ;
  83. while(BellmanFord(s,t,flow,cost)) ;
  84. return cost;
  85. }
  86. int dis(int x,int y,int xx,int yy)
  87. {
  88. return abs(x-xx)+abs(y-yy);
  89. }
  90. };
  91. char s[N][N];
  92. typedef pair<int,int> P;
  93. vector<P> man;
  94. vector<P> hou;
  95. int main()
  96. {
  97. int n, m;
  98. while(scanf("%d%d",&n,&m)==)
  99. {
  100. if(n==&&m==) break;
  101. man.clear();
  102. hou.clear();
  103. for(int i = ; i < n; i++){
  104. scanf("%s",s[i]);
  105. for(int j = ; j < m; j++){
  106. if(s[i][j]=='m'){
  107. man.push_back(P(i,j));
  108. }
  109. if(s[i][j]=='H'){
  110. hou.push_back(P(i,j));
  111. }
  112. }
  113. }
  114. MCMF mcmf;
  115. mcmf.s = ;
  116. mcmf.t = man.size()*+;
  117. mcmf.init(mcmf.t);
  118. for(int i = ; i < man.size(); i++){/// s->man
  119. mcmf.AddEdge(mcmf.s,i+,,);
  120. }
  121. for(int i = ; i < hou.size(); i++){/// hou->t
  122. mcmf.AddEdge(man.size()+i+,mcmf.t,,);
  123. }
  124. ///man->hou
  125. for(int i = ; i < man.size(); i++){
  126. for(int j = ; j < hou.size(); j++){
  127. int from, to, cap, cost;
  128. from = i+;
  129. to = man.size()+j+;
  130. cap = ;
  131. cost = mcmf.dis(man[i].first,man[i].second,hou[j].first,hou[j].second);
  132. mcmf.AddEdge(from,to,cap,cost);
  133. }
  134. }
  135. printf("%d\n",mcmf.Mincost(mcmf.s,mcmf.t));
  136. }
  137. return ;
  138. }

hdu1533 Going Home 最小费用最大流 构造源点和汇点的更多相关文章

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

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

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

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

  3. HIT2739 The Chinese Postman Problem(最小费用最大流)

    题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...

  4. BZOJ-1061 志愿者招募 线性规划转最小费用最大流+数学模型 建模

    本来一眼建模,以为傻逼题,然后发现自己傻逼...根本没想到神奇的数学模型..... 1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 ...

  5. 最小费用最大流 POJ2195-Going Home

    网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn. ...

  6. 【进阶——最小费用最大流】hdu 1533 Going Home (费用流)Pacific Northwest 2004

    题意: 给一个n*m的矩阵,其中由k个人和k个房子,给每个人匹配一个不同的房子,要求所有人走过的曼哈顿距离之和最短. 输入: 多组输入数据. 每组输入数据第一行是两个整型n, m,表示矩阵的长和宽. ...

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

    http://poj.org/problem?id=2195 题意 :  N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...

  8. [BZOJ2324][ZJOI2011][最小费用最大流]营救皮卡丘

    [Problem Description] 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队 ...

  9. POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

    POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...

随机推荐

  1. inline-block 前世今生(转)

    曾几何时,display:inline-block 已经深入「大街小巷」,随处可见 「display:inline-block; *display:inline; *zoom:1; 」这样的代码.如今 ...

  2. 安网讯通签约孟强美容CRM

    整形美容CRM软件是辽宁安网讯通有限公司为孟强整形医院定制开发的一套客户关系管理软件,软件专门针对整形美容行业专科门诊的需求,能满足大中小整形美容机构或或各种专科科室的日常业务需求. 主要功能包括: ...

  3. Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface解决方案

    原文:http://pimin.net/archives/432 环境:Eclipse LUNA.Spring 4.1.1.或Spring 4.3.3.Spring Data Elasticsearc ...

  4. VB6的UTF8编码解码

    'UTF-8编码  Public Function UTF8Encode(ByVal szInput As String) As String     Dim wch  As String     D ...

  5. css3的cursor

    1.cursor属性参考表 还有zoom-in/zoom-out 还有grab/grabbing 2.css (1)前面的基本上就 .xx { cursor: pointer; } (2)后面两个有兼 ...

  6. 腾讯云会话服务器node+nginx

    1.除了一个正常的服务器还需要一个会话服务器(websocket),利用node加socket.io来做 2.正常安装Nginx yum install nginx 3.Nginx的配置内容略微不同( ...

  7. SQL 日期格式化函数

    Sql Server 中一个非常强大的日期格式化函数: 获得当前系统时间,GETDATE(): 2008年01月08日 星期二 14:59 Select CONVERT(varchar(100), G ...

  8. RMAN 备份恢复 删除表空间后控制文件丢失

    先备份一个控制文件 RMAN> backup current controlfile tag='bak_ctlfile' format='/home/oracle/backup/bak_ctl_ ...

  9. cpu压力测试

    一.cpu压力测试 1.安装stress软件 sudo apt-get install stress #加压 nohup stress --cpu 8 & #查看cpu负载 top

  10. numpy基础知识

    官网简介: http://www.numpy.org/ ndarry基本属性 ndarry是Numpy中的N维数组对象(N dimentional arrya,ndarray) ndarry中所有的元 ...