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个. 分析: 上网搜了一下,发现很多人用网络流做的,发现我不会.再后来看到这篇说网络流的做法是错的,囧. 后来发现点数有点少,直接 ...
随机推荐
- Chapter 2 Open Book——7
I gunned my deafening engine to life, ignoring the heads that turned inmy direction, and backed care ...
- int与byte的区别
Java中涉及byte.short和char类型的运算操作首先会把这些值转换为int类型,然后对int类型值进行运算,最后得到int类型的结果.因此,如果把两个byte类型值相加,最后会得到一个int ...
- hdu_5695_Gym Class(拓扑排序)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5695 题意:中文题,不解释 题解:逆向拓扑字典序就行 #include<cstdio> # ...
- token 入门教程
下载 先去Apache下载一个tomcat 启动 进到安装目录的bin目录运行startup.bat,D:\apache-tomcat-8.0.33\bin (如果双击startup.bat一会自动关 ...
- 【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
[转载]CentOS 6.4下PXE+Kickstart无人值守安装操作系统 转自:CentOS 6.4下PXE+Kickstart无人值守安装操作系统 - David_Tang - 博客园 http ...
- 近十年one-to-one最短路算法研究整理【转】
前言:针对单源最短路算法,目前最经典的思路即标号算法,以Dijkstra算法和Bellman-Ford算法为根本演进了各种优化技术和算法.针对复杂网络,传统的优化思路是在数据结构和双向搜索上做文章,或 ...
- First()、FirstOrDefault()、Single() 和 SingleOrDefault()的区别
Enumerable.First() 方法:返回序列中的第一个元素,如果源序列为空,则抛异常. Enumerable.FirstOrDefault ()方法返回序列中的第一个元素:如果序列中不包含任何 ...
- linux学习的哲学层面的思考-架构
参考:http://blog.chinaunix.net/uid-26119273-id-3356414.html 学习Linux,准备做产品的话,不要把Linux当成了终极目标(当然,这是对应用而言 ...
- 【0-1 背包模板】 poj 3624
先看个未经优化的二维空间dp: #include <iostream> #include <cstdio> #include <cmath> #include &l ...
- 如何使用Android中的OpenGL ES媒体效果
引自:http://www.2cto.com/kf/201506/404366.html Android的媒体效果框架允许开发者可以很容易的应用多种令人印象深刻的视觉效果到照片或视频之上.作为这个媒体 ...