HDU 2485 Destroying the bus stations(费用流)
http://acm.hdu.edu.cn/showproblem.php?pid=2485
题意:
现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要破坏多少个车站。
思路:
把每个点拆分为两个点,容量为1,费用为0。之后相邻的车站连边,容量为INF,费用为1,表示经过一个车站需要1时间。
这样一来,跑一遍费用流计算出在费用不大于k的情况下的最大流,也就是最小割,即至少要破坏的车站数。
在网络中寻求关于f的最小费用增广路,就等价于在伴随网络中寻求从Vs到Vt的最短路。
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<sstream>
- #include<vector>
- #include<stack>
- #include<queue>
- #include<cmath>
- #include<map>
- #include<set>
- using namespace std;
- typedef long long ll;
- typedef long long ull;
- typedef pair<int,int> pll;
- const int INF = 0x3f3f3f3f;
- const int maxn = + ;
- int n, m, k;
- struct Edge
- {
- int from, to, cap, flow, cost;
- Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
- };
- struct MCMF
- {
- int n, m;
- vector<Edge> edges;
- vector<int> G[maxn];
- int inq[maxn];
- int d[maxn];
- int p[maxn];
- int a[maxn];
- void init(int n)
- {
- this->n = n;
- for (int i = ; i<n; i++) G[i].clear();
- edges.clear();
- }
- void AddEdge(int from, int to, int cap, int cost)
- {
- edges.push_back(Edge(from, to, cap, , cost));
- edges.push_back(Edge(to, from, , , -cost));
- m = edges.size();
- G[from].push_back(m - );
- G[to].push_back(m - );
- }
- bool BellmanFord(int s, int t, int &flow, int & cost)
- {
- for (int i = ; i<n; i++) d[i] = INF;
- memset(inq, , sizeof(inq));
- d[s] = ; inq[s] = ; p[s] = ; a[s] = INF;
- queue<int> Q;
- Q.push(s);
- while (!Q.empty()){
- int u = Q.front(); Q.pop();
- inq[u] = ;
- for (int i = ; i<G[u].size(); i++){
- Edge& e = edges[G[u][i]];
- if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
- d[e.to] = d[u] + e.cost;
- p[e.to] = G[u][i];
- a[e.to] = min(a[u], e.cap - e.flow);
- if (!inq[e.to]) { Q.push(e.to); inq[e.to] = ; }
- }
- }
- }
- if(d[t]>k) return false; //增广到大于k的时候就可以结束了
- if (d[t] == INF) return false;
- flow += a[t];
- cost += d[t] * a[t];
- for (int u = t; u != s; u = edges[p[u]].from)
- {
- edges[p[u]].flow += a[t];
- edges[p[u] ^ ].flow -= a[t];
- }
- return true;
- }
- int MincostMaxdflow(int s, int t){
- int flow = , cost = ;
- while (BellmanFord(s, t, flow, cost));
- return flow;
- }
- }t;
- int main()
- {
- //freopen("in.txt","r",stdin);
- while(~scanf("%d%d%d",&n,&m,&k) &&n && m &&k)
- {
- int src=,dst=n;
- t.init(*n);
- for(int i=;i<n;i++) t.AddEdge(i,i+n,,);
- for(int i=;i<m;i++)
- {
- int u,v;
- scanf("%d%d",&u,&v);
- if(u==src) t.AddEdge(u,v,INF,);
- else if(u==dst) continue;
- else t.AddEdge(u+n,v,INF,);
- }
- printf("%d\n",t.MincostMaxdflow(src,dst));
- }
- return ;
- }
HDU 2485 Destroying the bus stations(费用流)的更多相关文章
- 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 ...
- 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...
- hdu 2485 Destroying the bus stations 最小费用最大流
题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...
- HDU 2485 Destroying the bus stations (IDA*+ BFS)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...
- HDU 2485 Destroying the bus stations
2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...
- HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)
Destroying the bus stations ...
- Destroying the bus stations
Destroying the bus stations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1832 Acce ...
- HDU 5988 Coding Contest(浮点数费用流)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5988 题意:在acm比赛的时候有多个桌子,桌子与桌子之间都有线路相连,每个桌子上会有一些人和一些食物 ...
- HDU 4780 Candy Factory(拆点费用流)
Problem Description A new candy factory opens in pku-town. The factory import M machines to produc ...
随机推荐
- 巡风代码架构简介以及Flask的项目文件结构简介
一.巡风: 巡风是一款什么东西,想必安全同行都不陌生吧.用它作为内网漏洞扫描管理架构是一种很好的选择,扫描快,开源,还可自己编写符合规则的POC直接放入相应目录来扩展.今天下午趁着有点时间捋了一下巡风 ...
- 【BZOJ2300】[HAOI2011]防线修建 set维护凸包
[BZOJ2300][HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可 ...
- java的this表示当前类还是当前实例?
转自:http://www.runoob.com/java/java-basic-syntax.html this 表示调用当前实例或者调用另一个构造函数
- 设备加速传感器(accelerometer) ---- HTML5+
模块:Accelerometer Accelerometer模块管理设备加速度传感器,用于获取设备加速度信息,包括x(屏幕水平方向).y(垂直屏幕水平方向).z(垂直屏幕平面方向)三个方向的加速度信息 ...
- rest_framework之版本详解 04
访问不同版本,给不同内容. rest规范:版本要么放url上:要么放请求头里. 1.原来的url的版本都写死了.只能写v1 from django.conf.urls import url from ...
- 9.SQL存储过程实例详解
本文用3个题目,从建立数据库到创建存储过程,详细讲解数据库的功能. 题目1 学校图书馆借书信息管理系统建立三个表:学生信息表:student 字段名称 数据类型 说明 stuID char(10) 学 ...
- java 获取当前进程id 线程id
java 获取当前进程id 线程id RuntimeMXBean (Java Platform SE 8 ) https://docs.oracle.com/javase/8/docs/api/j ...
- CVP沙龙
关于职场: 35岁之后,还去招聘网站投简历? 35岁可能是个分水岭 95后比一些80后还强, 有些80后玻璃心 35岁有的可能已经是VP了 应该深入积累而不是蜻蜓点水 只有第一年成长了,之后是重复劳动 ...
- 一只青蛙从第一级台阶跳到第n级,每次可以跳任意级,共有多少种跳法,并写出递推式
是斐波那契数列问题 假设f(n)是n个台阶跳的次数:(假设已经调到第n个台阶,最后一次是由哪个台阶跳上来的) f(n) = f(n-1)+f(n-2)+...+f(n-(n-1)) + f(n-n) ...
- python-pdf添加水印
0.用到两个扩展模块:ReportLab.PyPDF2. 1.创建水印PDF. 1).创建文字水印pdf文件 代码: #encoding=utf-8 #author: walker #date: 20 ...