HDU 2485 Destroying the bus stations
2015 ACM / ICPC 北京站 热身赛 C题
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std; const int INF=0x7FFFFFFF;
const int maxn=+;//点的数量
int n,m,k;
int u[+],v[+];
int dis1[maxn],dis2[maxn];
bool flag1[maxn],flag2[maxn];
vector<int>P[maxn];
vector<int>FP[maxn];
queue<int>Q1;
queue<int>Q2; struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) :from(u), to(v), cap(c), flow(f) {}
};
vector<Edge>edges;
vector<int>G[maxn*];
bool vis[maxn*];
int d[maxn*];
int cur[maxn*];
int s, t; void read()
{
for(int i=;i<=m;i++)
{
scanf("%d%d",&u[i],&v[i]);
P[u[i]].push_back(v[i]);
FP[v[i]].push_back(u[i]);
}
} void init()
{
for(int i=;i<maxn;i++) P[i].clear();
for(int i=;i<maxn;i++) FP[i].clear(); for(int i=;i<=n;i++) dis1[i]=INF,dis2[i]=INF; for(int i = ; i < *maxn; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge(from, to, cap, ));
edges.push_back(Edge(to, from, , ));
int w = edges.size();
G[from].push_back(w - );
G[to].push_back(w - );
} bool BFS()
{
memset(vis, , sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while (!Q.empty())
{
int x = Q.front();
Q.pop();
for (int i = ; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (!vis[e.to] && e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x, int a)
{
if (x == t || a == )
return a;
int flow = , f;
for (int &i = cur[x]; i<G[x].size(); i++)
{
Edge e = edges[G[x][i]];
if (d[x]+ == d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
edges[G[x][i]].flow+=f;
edges[G[x][i] ^ ].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[x] = -;
return flow;
} int dinic(int s, int t)
{
int flow = ;
while (BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} void spfa1(int x)
{
while(!Q1.empty()) Q1.pop();
memset(flag1,,sizeof flag1); dis1[x]=;
flag1[x]=;
Q1.push(x); while(!Q1.empty())
{
int h=Q1.front(); flag1[h]=; Q1.pop();
for(int i=;i<P[h].size();i++)
{
if(dis1[h]+<dis1[P[h][i]])
{
dis1[P[h][i]]=dis1[h]+;
if(flag1[P[h][i]]==)
{
flag1[P[h][i]]=;
Q1.push(P[h][i]);
}
}
}
}
} void spfa2(int x)
{
while(!Q2.empty()) Q2.pop();
memset(flag2,,sizeof flag2); dis2[x]=;
flag2[x]=;
Q2.push(x); while(!Q2.empty())
{
int h=Q2.front(); flag2[h]=; Q2.pop();
for(int i=;i<FP[h].size();i++)
{
if(dis2[h]+<dis2[FP[h][i]])
{
dis2[FP[h][i]]=dis2[h]+;
if(flag2[FP[h][i]]==)
{
flag2[FP[h][i]]=;
Q2.push(FP[h][i]);
}
}
}
}
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
if(!n&&!m&&!k) break; init();
read(); spfa1();
spfa2(n); s=;t=n+n; for(int i=;i<=n;i++) AddEdge(i+n,i,); for(int i=;i<=m;i++)
if(dis1[u[i]]+dis2[v[i]]+<=k)
AddEdge(u[i],v[i]+n,INF); int ans=dinic(s,t);
printf("%d\n",ans);
}
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 (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 Destroying the bus stations 最小费用最大流
题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...
- 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 ...
- Destroying the bus stations HDU - 2485(最小割点)
题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa 然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...
- POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索
题目:给出一个图,问最少删除多少个点,使得从点1到点n经过的点数超过k个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...
随机推荐
- 100个精选zencart扩展插件
100个精选zencart扩展插件 特别推荐 1. 数据库备份 2. 产品横向布局. 3. 邮件订阅Newsletter Subscribe. 4. google 翻译google_translate ...
- iOS 开发者旅途中的指南针 - LLDB 调试技术
文章转载于:iOS 开发者旅途中的指南针 - LLDB 调试技术 今天给大家介绍的内容,无关乎任何功能性开发技术,但又对开发的效率影响至深,这就是调试技术. 何为调试呢,比如我们用 print 函数在 ...
- docker 基础命令二
开启/停止/重启 查看当前正在运行容器docker ps 查看包括已经停止的所有容器docker ps -a 显示最新启动的一个容器docker ps -l 新建一个容器运行docker run 启动 ...
- Linux 配置脚本 启动服务
之前在mac安装了php和nginx每次都用一堆命令重启 今天没事情干,心血来潮,自己研究写了一段shell脚本来重启 首先vim /usr/sbin/pn 代码如下 #! /bin/bash php ...
- PHP扩展开发-简单类扩展
今天来学习简单类扩展开发 实现目标为如下php的类 <?php class classext(){ private $username; CONST URL="http://www.g ...
- pro文件常用内容
qmake生成的pro文件中常用变量 SUBDIRS 指定子目录 TARGET 指定生成的应用程序名(默认为项目名) DEPENDPATH 指定程序编译时依赖的相关路径 INCLUDEPATH 指定头 ...
- inno setup 1
1.简单脚本 [setup] AppName=Test AppVerName=Test DefaultDirName="d:\setup\app" AppVersion=1.0 ...
- Zabbix3.0 安装Graphtree
zabbix中,想要集中展示图形,唯一的选择是screen,zatree可以解决这个问题,但是性能不是很好. Graphtree由OneOaas开发并开源出来,用来解决zabbix的图形展示问题,性能 ...
- 获取表空间的语句 以及 建表和索引的ddl
alter session set container=PHD1; SET SERVEROUTPUT ON SET LINESIZE SET FEEDBACK OFF SET PAGESIZE sel ...
- 【递归与分治】 poj 1017
递归与分治经典例题 要点在于对3*3箱子的讨论 #include <iostream> #include <cstdio> using namespace std; in ...