http://acm.hdu.edu.cn/showproblem.php?pid=1533

这道题直接用了模板

题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用

要注意void init(int n) 这个函数一定要写

一开始忘记写这个WA了好几发

还有这个题很容易T掉,赋值建图要简化,一开始构建成网络流那种图一直T

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

HDU 1533 最小费用最大流(模板)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

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

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

  9. hdu 4862KM&最小费用最大流

    /*最小K路径覆盖的模型,用费用流或者KM算法解决, 构造二部图,X部有N*M个节点,源点向X部每个节点连一条边, 流量1,费用0,Y部有N*M个节点,每个节点向汇点连一条边,流量1, 费用0,如果X ...

随机推荐

  1. 注释(Annotation)

    J2SE 5.0提供了很多新的特征.其中一个很重要的特征就是对元数据(Metadata)的支持.在J2SE 5.0中,这种元数据称为注释(Annotation).通过使用注释,程序开发人员可以在不改变 ...

  2. strcpy函数的实现

    strcpy函数的实现 大家一般认为名不见经传strcpy函数实现不是很难,流行的strcpy函数写法是: char *my_strcpy(char *dst,const char *src) { a ...

  3. 第六章 jQuery和ajax应用

    ajax是异步JavaScript和xml的简称. 一. ajax补白 优势 不足(不一定是不足) 不需要任何插件(但需要浏览器支持js) XMLHttpRequest对象在不同浏览器下有差异 优秀的 ...

  4. Swift学习二

    // 定义枚举方式一 enum Season { // 每个case定义一个实例 case Spring case Summer case Fall case Winter } // 定义枚举方式二 ...

  5. OWIN support for the Web API 2 and MVC 5 integrations in Autofac

    Currently, in the both the Web API and MVC frameworks, dependency injection support does not come in ...

  6. js中,还真不了解 console

    参考链接: https://segmentfault.com/a/1190000000481884

  7. C#中(int)a和Convert.ToInt32(a)有什么区别

    首先,在 C# 中,int 其实就是 System.Int32,即都是32位的. 其次,(int) 和 Convert.ToInt32 是两个不同的概念,前者是类型转换,而后者则是内容转换,它们并不总 ...

  8. 关于 jquery 选择器的 深入理解 -1

    多级选择器: 前面一个selector1, 后面通过 //空格, >, + ~, 各种筛选 选择器 + selector2 // 再次进行选择的,就叫做多级选择器 jquery的一个基本常识: ...

  9. winxp可以禁用的服务

    要注意的是: 虽然某个服务你设置成了手动, 而且在services.msc中好像也没有启动, 但是并不表示这个服务不可以被启动 因为某些软件, 可能在程序内部进行了编程的设置, 它可以在内部去启动 服 ...

  10. Ubuntu 14 安装 “宋体,微软雅黑,WPS Office的symbol、wingdings、wingdings 2、wingdings 3、webding字体,Consolas雅黑混合版编程字体” 等 Windows 7 下的字体

    Windows平台下,“宋体”.“微软雅黑”.“Courier New(编程字体)”用的比较多,看的也习惯了.那如何在 Ubuntu下也安装这些字体呢? 操作步骤如下: 第一步:从 Windows 7 ...