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

题意:

现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要破坏多少个车站。

思路:

把每个点拆分为两个点,容量为1,费用为0。之后相邻的车站连边,容量为INF,费用为1,表示经过一个车站需要1时间。

这样一来,跑一遍费用流计算出在费用不大于k的情况下的最大流,也就是最小割,即至少要破坏的车站数。

在网络中寻求关于f的最小费用增广路,就等价于在伴随网络中寻求从Vs到Vt的最短路。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<sstream>
  6. #include<vector>
  7. #include<stack>
  8. #include<queue>
  9. #include<cmath>
  10. #include<map>
  11. #include<set>
  12. using namespace std;
  13. typedef long long ll;
  14. typedef long long ull;
  15. typedef pair<int,int> pll;
  16. const int INF = 0x3f3f3f3f;
  17. const int maxn = + ;
  18.  
  19. int n, m, k;
  20.  
  21. struct Edge
  22. {
  23. int from, to, cap, flow, cost;
  24. Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
  25. };
  26.  
  27. struct MCMF
  28. {
  29. int n, m;
  30. vector<Edge> edges;
  31. vector<int> G[maxn];
  32. int inq[maxn];
  33. int d[maxn];
  34. int p[maxn];
  35. int a[maxn];
  36.  
  37. void init(int n)
  38. {
  39. this->n = n;
  40. for (int i = ; i<n; i++) G[i].clear();
  41. edges.clear();
  42. }
  43.  
  44. void AddEdge(int from, int to, int cap, int cost)
  45. {
  46. edges.push_back(Edge(from, to, cap, , cost));
  47. edges.push_back(Edge(to, from, , , -cost));
  48. m = edges.size();
  49. G[from].push_back(m - );
  50. G[to].push_back(m - );
  51. }
  52.  
  53. bool BellmanFord(int s, int t, int &flow, int & cost)
  54. {
  55. for (int i = ; i<n; i++) d[i] = INF;
  56. memset(inq, , sizeof(inq));
  57. d[s] = ; inq[s] = ; p[s] = ; a[s] = INF;
  58.  
  59. queue<int> Q;
  60. Q.push(s);
  61. while (!Q.empty()){
  62. int u = Q.front(); Q.pop();
  63. inq[u] = ;
  64. for (int i = ; i<G[u].size(); i++){
  65. Edge& e = edges[G[u][i]];
  66. if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
  67. d[e.to] = d[u] + e.cost;
  68. p[e.to] = G[u][i];
  69. a[e.to] = min(a[u], e.cap - e.flow);
  70. if (!inq[e.to]) { Q.push(e.to); inq[e.to] = ; }
  71. }
  72. }
  73. }
  74.  
  75. if(d[t]>k) return false; //增广到大于k的时候就可以结束了
  76. if (d[t] == INF) return false;
  77. flow += a[t];
  78. cost += d[t] * a[t];
  79. for (int u = t; u != s; u = edges[p[u]].from)
  80. {
  81. edges[p[u]].flow += a[t];
  82. edges[p[u] ^ ].flow -= a[t];
  83. }
  84. return true;
  85. }
  86.  
  87. int MincostMaxdflow(int s, int t){
  88. int flow = , cost = ;
  89. while (BellmanFord(s, t, flow, cost));
  90. return flow;
  91. }
  92. }t;
  93.  
  94. int main()
  95. {
  96. //freopen("in.txt","r",stdin);
  97. while(~scanf("%d%d%d",&n,&m,&k) &&n && m &&k)
  98. {
  99. int src=,dst=n;
  100. t.init(*n);
  101.  
  102. for(int i=;i<n;i++) t.AddEdge(i,i+n,,);
  103.  
  104. for(int i=;i<m;i++)
  105. {
  106. int u,v;
  107. scanf("%d%d",&u,&v);
  108. if(u==src) t.AddEdge(u,v,INF,);
  109. else if(u==dst) continue;
  110. else t.AddEdge(u+n,v,INF,);
  111. }
  112. printf("%d\n",t.MincostMaxdflow(src,dst));
  113. }
  114. return ;
  115. }

HDU 2485 Destroying the bus stations(费用流)的更多相关文章

  1. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  2. 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)

    Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...

  3. hdu 2485 Destroying the bus stations 最小费用最大流

    题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...

  4. HDU 2485 Destroying the bus stations (IDA*+ BFS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...

  5. HDU 2485 Destroying the bus stations

    2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...

  6. HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)

    Destroying the bus stations                                                                          ...

  7. Destroying the bus stations

    Destroying the bus stations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1832   Acce ...

  8. HDU 5988 Coding Contest(浮点数费用流)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...

  9. HDU 4780 Candy Factory(拆点费用流)

    Problem Description   A new candy factory opens in pku-town. The factory import M machines to produc ...

随机推荐

  1. 巡风代码架构简介以及Flask的项目文件结构简介

    一.巡风: 巡风是一款什么东西,想必安全同行都不陌生吧.用它作为内网漏洞扫描管理架构是一种很好的选择,扫描快,开源,还可自己编写符合规则的POC直接放入相应目录来扩展.今天下午趁着有点时间捋了一下巡风 ...

  2. 【BZOJ2300】[HAOI2011]防线修建 set维护凸包

    [BZOJ2300][HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可 ...

  3. java的this表示当前类还是当前实例?

    转自:http://www.runoob.com/java/java-basic-syntax.html this 表示调用当前实例或者调用另一个构造函数

  4. 设备加速传感器(accelerometer) ---- HTML5+

    模块:Accelerometer Accelerometer模块管理设备加速度传感器,用于获取设备加速度信息,包括x(屏幕水平方向).y(垂直屏幕水平方向).z(垂直屏幕平面方向)三个方向的加速度信息 ...

  5. rest_framework之版本详解 04

    访问不同版本,给不同内容. rest规范:版本要么放url上:要么放请求头里. 1.原来的url的版本都写死了.只能写v1 from django.conf.urls import url from ...

  6. 9.SQL存储过程实例详解

    本文用3个题目,从建立数据库到创建存储过程,详细讲解数据库的功能. 题目1 学校图书馆借书信息管理系统建立三个表:学生信息表:student 字段名称 数据类型 说明 stuID char(10) 学 ...

  7. java 获取当前进程id 线程id

    java  获取当前进程id  线程id RuntimeMXBean (Java Platform SE 8 ) https://docs.oracle.com/javase/8/docs/api/j ...

  8. CVP沙龙

    关于职场: 35岁之后,还去招聘网站投简历? 35岁可能是个分水岭 95后比一些80后还强, 有些80后玻璃心 35岁有的可能已经是VP了 应该深入积累而不是蜻蜓点水 只有第一年成长了,之后是重复劳动 ...

  9. 一只青蛙从第一级台阶跳到第n级,每次可以跳任意级,共有多少种跳法,并写出递推式

    是斐波那契数列问题 假设f(n)是n个台阶跳的次数:(假设已经调到第n个台阶,最后一次是由哪个台阶跳上来的) f(n) = f(n-1)+f(n-2)+...+f(n-(n-1)) + f(n-n) ...

  10. python-pdf添加水印

    0.用到两个扩展模块:ReportLab.PyPDF2. 1.创建水印PDF. 1).创建文字水印pdf文件 代码: #encoding=utf-8 #author: walker #date: 20 ...