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. scss语法

    SCSS其实就是SASS新语法, 增强了对CSS3语法的支持 1.变量(Variables) /*声明变明*/ $color: #333; $bgcolor:#f36; /*引用变量*/ body { ...

  2. Weinre 远程调试移动端手机web页面

    调试场景 1.调试页面在手机上.2.调试工具在PC的chrome3.手机跟pc要在同一个网络环境下,也就是都使用一个wifi 一.安装 Weinre 1.Weinre是基于nodejs实现的,所以使用 ...

  3. console输出图案

    探索天猫控制台下的图案是怎么制作的 通过它的源码找到以下代码(还原解压代码) 自己也照葫画瓢搞了个以前公司的logo

  4. go http 文件下载

    package main import ( "fmt" "net/http" "os" ) func DownFile() { userFi ...

  5. Linux系统下 Apache+PHP 环境安装搭建

    一.安装Apache2.2.221.到官网下载  http://httpd.apache.org/download.cgi ,选择相应的版本 可以先下载到windows系统中,上传到linux, 也可 ...

  6. Java的Object.hashCode()的返回值到底是不是对象内存地址?

    关于这个问题,查阅了网上的资料,发现证明过程太繁琐,这里我用了反证法. java.lang.Object.hashCode()的返回值到底是不是对象内存地址? hashCode契约 说到这个问题,大家 ...

  7. Css-常用css初始化

    /*PC初始化*/ * {;;; } body, html { width: 100%; height: 100%; min-width: 1024px; } body { font-size: 14 ...

  8. Oracle下Delete语句

    Delete语句 基本语法: delete from 表名 where 条件 注意事项: 1,如果不使用where子句,将表中所有数据全部删除 delete from test; 2,如果要删除某列的 ...

  9. linux内核cdev_init系列函数(字符设备的注册)

    内核中每个字符设备都对应一个 cdev 结构的变量,下面是它的定义: linux-2.6.22/include/linux/cdev.h struct cdev {    struct kobject ...

  10. python初识(三)

    目录: 字符编码 文件操作 循环结构拾遗 函数 整体介绍 函数的参数 函数的返回值 函数的调用 自定义函数 一.字符编码 1.了解字符编码的知识储备 a. 文件编辑存取文件的原理(nodepad++, ...