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(费用流)的更多相关文章

  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. CRUX下实现进程隐藏(2)

    前面我们介绍了如何修改/proc目录读取函数的方法实现进程隐藏.这篇博文将介绍另一种方法—— 劫持系统调用实现进程隐藏. 其基本原理是:加载一个内核模块(LKM),通过劫持系统调用sys_getden ...

  2. 在RES.web.Html5VersionController废弃后,如何做版本管理

    在之前的版本,可以通过重写Html5VersionController, 在游戏一次更新后,增加v版本号,来达到修改每次加载的png.mp3.json等文件的url不同,来解决缓存的问题. 这样的好处 ...

  3. 关于wcf三大工具的使用(wsdl.exe svcutil.exe disco.exe)

    首先,我们必须创建一个wcf服务.并部署到IIS中.这里我已经将一个StudentService服务部署到我自己的电脑了. (1)svcutil.exe svcutil.exe工具的作用是通过服务地址 ...

  4. python下几种打开文件的方式

    昨天看完了这本python进阶,感觉这本书对我启发很大,做了三张纸的笔记,方便我在遇到问题的时候翻阅,然后寻找可能的解决方案.作为一个使用Python一年的小白,虽然说不是小白,但是这一年来基本上是用 ...

  5. postgresql----Btree索引

    当表数据量越来越大时查询速度会下降,像课本目录一样,在表的条件字段上创建索引,查询时能够快速定位感兴趣的数据所在的位置.索引的好处主要有加速带条件的查询,删除,更新,加速JOIN操作,加速外键约束更新 ...

  6. 170629、springboot编程之Druid数据源和监控配置二

    上篇是一种配置方式,虽然我们创建了servlet.filter但是没有任务编码,看着是不是很不爽.ok,接下来说一下简介的配置方式,使用代码注册Servlet,也是我个人比较推荐的! 1.创建Drui ...

  7. xplan-打印执行顺序

    -- ------------------------------------------------------------------------------------------------- ...

  8. ruby rvm groke

    https://ruby-china.org/wiki/rvm-guide http://www.cnblogs.com/peak-c/p/7338291.html https://doc.yonyo ...

  9. day12(jsp指令&内置对象&动作标签、JavaBean、EL表达式&函数库)

    day12 JSP指令 JSP指令概述 JSP指令的格式:<%@指令名 attr1="" attr2="" %>,一般都会把JSP指令放到JSP文件 ...

  10. React Native入门-刘望舒

    React Native入门(一)环境搭建与Hello World React Native入门(二)Atom+Nuclide安装.配置与调试 React Native入门(三)组件的Props(属性 ...