Going Home

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

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
 
大意:将两个点配对的花费为其曼哈顿距离,问将每个H与m配对的最小花费。
 
 
 
 
 
 
 
题解:建二分图,带权匹配(KM暂时不会)。
一切皆可网络流,也可以用费用流写,这题就当存个模板了。
 
  1. /*
  2. Welcome Hacking
  3. Wish You High Rating
  4. */
  5. #include<iostream>
  6. #include<cstdio>
  7. #include<cstring>
  8. #include<ctime>
  9. #include<cstdlib>
  10. #include<algorithm>
  11. #include<cmath>
  12. #include<string>
  13. using namespace std;
  14. int read(){
  15. int xx=0,ff=1;char ch=getchar();
  16. while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
  17. while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
  18. return xx*ff;
  19. }
  20. const int maxlongint=(1LL<<31)-1;
  21. inline int myabs(int xx)
  22. {if(xx<0)return -xx;return xx;}
  23. inline int mymin(int xx,int yy)
  24. {if(xx>yy)return yy;return xx;}
  25. inline int mymax(int xx,int yy)
  26. {if(xx>yy)return xx;return yy;}
  27. int N,M,st,en;
  28. int s1[110],s2[110],tp1,tp2,ans;
  29. inline int get_id(int xx,int yy)
  30. {return (xx-1)*M+yy;}
  31. inline int get_dis(int id1,int id2){
  32. int sx=id1/M,sy=id1%M,fx=id2/M,fy=id2%M;
  33. if(!sy)
  34. sx--,sy=M;
  35. if(!fy)
  36. fx--,fy=M;
  37. return myabs(sx-fx)+myabs(sy-fy);
  38. }
  39. int lin[210],len;
  40. struct edge{
  41. int y,next,v,f;
  42. }e[200010];
  43. inline void insert(int xx,int yy,int ff,int vv){
  44. e[++len].next=lin[xx];
  45. lin[xx]=len;
  46. e[len].y=yy;
  47. e[len].v=vv;
  48. e[len].f=ff;
  49. }
  50. inline void ins(int xx,int yy,int ff,int vv)
  51. {insert(xx,yy,ff,vv),insert(yy,xx,0,-vv);}
  52. void build(){
  53. st=tp1+tp2+1,en=st+1;
  54. memset(lin,0,sizeof(lin));len=0;
  55. for(int i=1;i<=tp1;i++)
  56. for(int j=1;j<=tp2;j++)
  57. ins(i,j+tp1,1,get_dis(s1[i],s2[j]));
  58. for(int i=1;i<=tp1;i++)
  59. ins(st,i,1,0);
  60. for(int j=1;j<=tp2;j++)
  61. ins(j+tp1,en,1,0);
  62. }
  63. int q[1000010],head,tail,dis[210],Prev[210],useedge[210];
  64. bool vis[210];
  65. bool SPFA(){
  66. memset(vis,0,sizeof(vis));
  67. memset(dis,10,sizeof(dis));
  68. head=tail=0;
  69. q[head]=st;
  70. vis[q[head]]=1;
  71. dis[q[head]]=0;
  72. for(;head<=tail;head++){
  73. vis[q[head]]=0;
  74. for(int i=lin[q[head]];i;i=e[i].next)
  75. if(e[i].f)
  76. if(dis[e[i].y]>dis[q[head]]+e[i].v){
  77. dis[e[i].y]=dis[q[head]]+e[i].v;
  78. if(!vis[e[i].y]){
  79. vis[e[i].y]=1;
  80. q[++tail]=e[i].y;
  81. }
  82. Prev[e[i].y]=q[head];
  83. useedge[e[i].y]=i;
  84. }
  85. }
  86. //printf("#%d#\n",dis[en]);
  87. return dis[en]!=dis[0];
  88. }
  89. void agu(){
  90. int add=maxlongint;
  91. for(int i=en;i!=st;i=Prev[i]){
  92. add=mymin(add,e[useedge[i]].f);
  93. }
  94. for(int i=en;i!=st;i=Prev[i]){
  95. e[useedge[i]].f-=add;
  96. if(useedge[i]&1)
  97. e[useedge[i]+1].f+=add;
  98. else
  99. e[useedge[i]-1].f+=add;
  100. ans+=add*e[useedge[i]].v;
  101. }
  102. }
  103. void cost_flow(){
  104. ans=0;
  105. while(SPFA())
  106. agu();
  107. printf("%d\n",ans);
  108. }
  109. int main(){
  110. //freopen("in","r",stdin);
  111. //freopen("out","w",stdout);
  112. while(1){
  113. N=read(),M=read();
  114. if((!N)&&(!M))
  115. break;
  116. tp1=tp2=0;
  117. char tmp;
  118. for(int i=1;i<=N;i++)
  119. for(int j=1;j<=M;j++){
  120. tmp=getchar();
  121. while(tmp==10||tmp==32)
  122. tmp=getchar();
  123. if(tmp=='H')
  124. s1[++tp1]=get_id(i,j);
  125. else if(tmp=='m')
  126. s2[++tp2]=get_id(i,j);
  127. }
  128. build();
  129. cost_flow();
  130. }
  131. return 0;
  132. }

  

hdu1533 费用流模板的更多相关文章

  1. HDU2686 费用流 模板

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. HDU 6611 K Subsequence(Dijkstra优化费用流 模板)题解

    题意: 有\(n\)个数\(a_1\cdots a_n\),现要你给出\(k\)个不相交的非降子序列,使得和最大. 思路: 费用流建图,每个点拆点,费用为\(-a[i]\),然后和源点连边,和后面非降 ...

  3. 费用流模板(带权二分图匹配)——hdu1533

    /* 带权二分图匹配 用费用流求,增加源点s 和 汇点t */ #include<bits/stdc++.h> using namespace std; #define maxn 1000 ...

  4. 初识费用流 模板(spfa+slf优化) 餐巾计划问题

    今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...

  5. 算法复习——费用流模板(poj2135)

    题目: Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16898   Accepted: 6543 De ...

  6. Spfa费用流模板

    ; ,maxm=; ,fir[maxn],nxt[maxm],to[maxm]; int cap[maxm],val[maxm],dis[maxn],path[maxn]; void add(int ...

  7. zkw费用流模板

    理论:http://www.cnblogs.com/acha/p/6735037.html #include<cstdio> #include<cstring> #includ ...

  8. 【费用流】【Next Array】费用流模板(spfa版)

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...

  9. 费用流 ZOJ 3933 Team Formation

    题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...

随机推荐

  1. win10 打开chm文件内容空白如何解决

    win10 打开chm文件内容空白如何解决 .CHM文件是非常常见的帮助文件格式.由于其便携性,很多小说或杂志也会采用chm格式.win7/win8.1/win10系统,由于采用了UAC,致使原本在x ...

  2. Python 之字符串操作

    # capitalize()将字符串的第一个字符转换为大写 # center(width, fillchar)返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格. ...

  3. loader__demo_css

    环境 node + yarn + webpack4.0 + webpack-cli + style-loader css-loader 文件结构 │ package.json │ webpack.co ...

  4. CentOS安装Docker-ce并配置国内镜像

    前提条件 1.系统.内核 CentOS7 要求64位系统.内核版本3.10以上 CentOS6 要求版本在6.5以上,系统64位.内核版本2.6.32-431以上 查看内核版本号 uname -r # ...

  5. P1002 过河卒 【递推、简单动规】

    题目描述 棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒 ...

  6. POJ 3984 迷宫问题 (BFS + Stack)

    链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...

  7. MySQL之视图、触发器、存储过程、函数、事务、数据库锁

    一.视图 视图:是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据. 视图的特点: 1.视图的列可以来自不同的表,是表的抽象和逻辑意义上建立的新关系: 2.视图是由基本 ...

  8. CODEVS 3500

    题目描述 输入3个数a,b,c,求a^b mod c=?输入描述          三个数a,b,c输出描述         一个数,即a^b mod c 的答案.样例输入5 10 9样例输出 4 基 ...

  9. Maven学习总结(9)——使用Nexus搭建Maven私服

    1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库:否则,私服请求外部 ...

  10. Spring MVC-表单(Form)标签-文件上传(File Upload)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_upload.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显 ...