HDU 2485
http://acm.hdu.edu.cn/showproblem.php?pid=2485
n个车站,m条边,两边之间费用为1,问最少摧毁多少车站,使得1-n无法在k时间内到达
将2-(n-1)每个点拆成两个,并建立容量为1,费用为0的一条边,源点为1,汇点为2*n-2,这时求最小费用最大流,其中保证dis[t]<=k,求得的最大流即为摧毁的车站数量(因为1流量代表能从其中一个车站到达n点)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std ;
const int INF=0xfffffff ;
struct node{
int s,t,cap,cost,nxt ;
}e[] ;
int sumflow ;
int n,m,k,cnt,head[],vis[],dis[],pre[] ;
void add(int s,int t,int cap,int cost)
{
e[cnt].s=s ;e[cnt].t=t ;e[cnt].cap=cap ;e[cnt].cost=cost ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
e[cnt].s=t ;e[cnt].t=s ;e[cnt].cap= ;e[cnt].cost=-cost ;e[cnt].nxt=head[t] ;head[t]=cnt++ ;
}
int spfa(int s,int t,int N)
{
for(int i= ;i<=N ;i++)
dis[i]=INF ;
dis[s]= ;
memset(vis,,sizeof(vis)) ;
memset(pre,-,sizeof(pre)) ;
vis[s]= ;
queue <int> q ;
q.push(s) ;
while(!q.empty())
{
int u=q.front() ;
q.pop() ;
vis[u]= ;
for(int i=head[u] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(e[i].cap && dis[tt]>dis[u]+e[i].cost)
{
dis[tt]=dis[u]+e[i].cost ;
pre[tt]=i ;
if(!vis[tt])
{
vis[tt]= ;
q.push(tt) ;
}
}
}
}
if(dis[t]==INF || dis[t]>k)return ;
return ;
}
int MCMF(int s,int t,int N)
{
int flow,minflow,mincost ;
mincost=flow= ;
while(spfa(s,t,N))
{
minflow=INF ;
for(int i=pre[t] ;i!=- ;i=pre[e[i].s])
minflow=min(minflow,e[i].cap) ;
flow+=minflow ;
for(int i=pre[t] ;i!=- ;i=pre[e[i].s])
{
e[i].cap-=minflow ;
e[i^].cap+=minflow ;
}
mincost+=dis[t]*minflow ;
}
sumflow=flow ;//最大流
return mincost ;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k))
{
if(!n && !m && !k)break ;
cnt= ;
memset(head,-,sizeof(head)) ;
int s= ;
int t=*n- ;
for(int i= ;i<n- ;i++)
add(*i,*i+,,) ;
while(m--)
{
int a,b ;
scanf("%d%d",&a,&b) ;
a-- ;b-- ;
add(a*+,b*,,) ;
}
MCMF(s,t,t) ;
printf("%d\n",sumflow) ;
}
return ;
}
HDU 2485的更多相关文章
- HDU(2485),最小割最大流
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485 Destroying the bus stations Time Limit: 40 ...
- 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(费用流)
http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要 ...
- hdu 2485(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2485 思路:题目的意思是删除最少的点使1,n的最短路大于k.将点转化为边,容量为1,费用为0,然后就是 ...
- HDU 2485 Destroying the bus stations
2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...
- Destroying the bus stations HDU - 2485(最小割点)
题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa 然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...
- 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 最小费用最大流
题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...
- 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...
随机推荐
- R语言编程
R中的帮助文档非常有用,其中有四种类型的帮助 help(functionname) 对已经加载包所含的函数显示其帮助文档,用?号也是一样的. help.search('keyword') 对已经安装的 ...
- Android开发新手问题
因为最近在用空闲时间学习Android开发,期间确实遇到了一些问题.而且因为我之前在公司里一直都是在使用Eclipse进行开发,所以最初我学习Android时也就选择了Google的包含android ...
- 1.初步认识JVM -- JVM序列
1.JVM概念 JVM是java Virtual Machine的简称.也称为Java虚拟机. 虚拟机:通过软件模拟具有完整硬件功能的运行在一个完全隔离环境的完整计算机系统.VMWare.Visual ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- uva 10254
如果我们设f[i]为4个柱子时把i个东东从一个柱子移到另一个柱子所用的最少步骤,设g[i]为3个柱子时对应的值,我们可以得到f[n]=min{2*f[k]+g[n-k]},其中g[i]是已知的为2^i ...
- linux下高并发网络应用注意事项
本文转自:http://www.blogjava.net/bacoo/archive/2012/06/11/380500.html linux下高并发网络应用注意事项 vi /etc/sysctl.c ...
- pug 在线文档
https://pugjs.org/zh-cn/api/getting-started.html
- express+mongodb+mongoose简单入门
mongodb安装 window安装方法就不讨论了,比较简单~我们来看一下在linux下面的安装步骤~(这里需要一点linux的简单命令知识哈) 1.下载文件到服务器(先创建好自己想安装的目录)~ c ...
- Spring-Boot 中 Redis 的简单使用以及简单模糊匹配删除
https://yulaiz.com/spring-boot-redis-simple/
- HTTP从入门到入土(2)——HTTP协议发展历史
HTTP协议的发展历史 HTTP/0.9 0.9版本是第一个定稿的HTTP版本,相对较为简陋.它有以下特点: 只有一个命令GET 没有header等描述数据的信息 服务器发送完毕,就关闭TCP连接 注 ...