dfs | Security Badges
Description You are in charge of the security for a large building, with n rooms and m doors between the rooms. The rooms and doors are conveniently numbered from 1 to n, and from 1 to m, respectively.
Door i opens from room ai to room bi , but not the other way around. Additionally, each door has a security code that can be represented as a range of numbers [ci , di ].
There are k employees working in the building, each carrying a security badge with a unique, integer-valued badge ID between 1 and k. An employee is cleared to go through door i only when the badge ID x satisfies ci ≤ x ≤ di .
Your boss wants a quick check of the security of the building. Given s and t, how many employees can go from room s to room t?
Input The first line of input contains three space-separated integers n, m, and k (2 ≤ n ≤ 1,000; 1 ≤ m ≤ 5,000; 1 ≤ k ≤ 109 ). The second line of input contains two space-separated integers s and t (1 ≤ s, t ≤ n; s 6= t). Each of the next m lines contains four space-separated integers ai , bi , ci , and di (1 ≤ ai , bi ≤ n; 1 ≤ ci ≤ di ≤ k; ai 6= bi), describing door i. For any given pair of rooms a, b there will be at most one door from a to b (but there may be both a door from a to b and a door from b to a).
Output Print, on a single line, the number of employees who can reach room t starting from room s.
题意 共有n间房间,m扇门,k位员工(编号为1~k)。每扇门连接两个房间,可以允许通过该门的员工编号范围称作code,用闭区间[ci,di]表示。求从房间s出发,最终共有多少人员可以到达房间t。
思路 dfs求出所有起点为s终点为t的路径,对于每一条路径,求出所有code的交集;对于所有路径,求出所有答案的并集。两种效率低下的朴素思路分别为:1.依次对每位员工判断是否能到达终点 2.对每条路径判断有哪些员工可以到达终点。针对第一种思路进行优化,可以将所有员工分为若干区间,同区间内的员工具有共同的性质,即能够一起通过某条路径到达终点,或一起被卡在某条路径的半途。
1 #include <iostream>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <memory.h>
5 #include <algorithm>
6
7 using namespace std;
8
9 //邻接表模板
10 typedef struct adjnode
11 {
12 int end;
13 int c;
14 int d;
15 struct adjnode *next;
16 } Node;
17 typedef struct adjlist
18 {
19 Node *head;
20 } List;
21 typedef struct _Graph
22 {
23 int vertices;
24 int edges;
25 List *array;
26 }Graph;
27
28 Graph* Create(int v)
29 {
30 Graph *graph=new Graph;
31
32 graph->vertices=v;
33 graph->edges=0;
34
35 graph->array=new List[v+1]; //0~v
36
37 for(int i=0;i<=v;i++){
38 graph->array[i].head=NULL;
39 }
40
41 return graph;
42 }
43
44 void AddEdge(Graph *graph,int begin,int end,int ci,int di)
45 {
46 Node *newnode=new Node;
47
48 newnode->end=end;
49 newnode->next=graph->array[begin].head;
50 newnode->c=ci;
51 newnode->d=di;
52 graph->array[begin].head=newnode;
53
54 graph->edges++;
55 }
56
57
58 int t,ans=0;
59 int edges[10005];
60
61 bool dfs(Graph *g,int cur,int l,int r,bool vis[1005]){
62 vis[cur]=true;
63
64 if(cur==t){
65 return true;
66 }
67
68 Node *p=g->array[cur].head;
69 while(p){
70 if(!vis[p->end] && p->c<=l && p->d>=r){
71 if(dfs(g,p->end,l,r,vis))
72 return true;
73 }
74 p=p->next;
75 }
76
77 return false;
78 }
79
80 int main()
81 {
82 int n,m,k,s;
83 cin>>n>>m>>k>>s>>t;
84
85 Graph *hotel=Create(n);
86
87 int a,b,u,v;
88 for(int i=0;i<m;i++){
89 cin>>a>>b>>u>>v;
90
91 AddEdge(hotel,a,b,u,v);
92 edges[i*2]=u;
93 edges[i*2+1]=v+1; //[a,b]∪[b+1,c]等同于[a,c]
94 }
95
96 sort(edges,edges+2*m);
97 int cnt=unique(edges,edges+2*m)-edges;
98
99 bool vis[1005];
100
101 for(int i=1;i<cnt;i++){
102 memset(vis,false,sizeof(vis));
103 if(dfs(hotel,s,edges[i-1],edges[i]-1,vis)) //规定受检区间左闭右开
104 ans+=(edges[i]-edges[i-1]);
105 }
106
107 cout<<ans<<endl;
108
109 return 0;
110 }
来源 2017-2018 acm-icpc northwest regional contest
参考 https://blog.csdn.net/yz467796454/article/details/78753171 ; 官方题解
dfs | Security Badges的更多相关文章
- 【离散化】【DFS】Gym - 101617H - Security Badges
题意:给你一张有向图,每条边有个限制范围,只有权值在限制范围内的人能走这条边,问你权值不超过K的人中,有多少人能从S到T. K很大,因此我们只处理边的范围的上下界这O(m)个权值能否到达,以防万一,还 ...
- DFS security warning and use group policy to set up internet security zones
Opening a file from a DFS domain share shows a security warning while openning from the server share ...
- UVA 10318 Security Panel(DFS剪枝 + 状压 + 思维)题解
题意:给一个r*c的矩阵开关(初始全打开的),每次按下一个开关都会改变3*3范围内的有*的地方的状态,问你最少几步能让开关全闭上,按升序输出按哪些按钮 思路:每个按钮至多按一下,按按钮的顺序和结果无关 ...
- UVA-10318 Security Panel (DFS+剪枝)
题目大意:求将一个r*c的按钮矩阵由全部为关变成全部为开的最少按扭次数,每按一次开关能作用到的范围不定. 题目分析:开关问题.打眼一看就是BFS+位压缩,但是写出来之后TLE.用DFS不断更新最优解. ...
- ERROR [org.apache.hadoop.security.UserGroupInformation] - PriviledgedActionExcep
换了个环境,出现此异常 016-10-18 23:54:01,334 WARN [org.apache.hadoop.util.NativeCodeLoader] - Unable to load n ...
- zoj3811 Untrusted Patrol (dfs)
2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...
- kylin cube测试时,报错:org.apache.hadoop.security.AccessControlException: Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup:drwxr-xr-x
异常: org.apache.hadoop.security.AccessControlException: Permission denied: user=root, access=WRITE, i ...
- org.apache.hadoop.security.AccessControlException: Permission denied:
org.apache.hadoop.security.AccessControlException: Permission denied: user=xxj, access=WRITE, inode= ...
- Valid Pattern Lock(dfs + 暴力)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
随机推荐
- Guava 6:Concurrency
一.引子 有点经验的工程师一定对多线程比较熟悉,JDK封装的FutureTask实现了这一功能.如下图: FutureTask实现了RunnableFuture接口,而RunnableFuture接口 ...
- [转]阿里巴巴十年Java架构师分享,会了这个知识点的人都去BAT了
1.源码分析专题 详细介绍源码中所用到的经典设计思想,看看大牛是如何写代码的,提升技术审美.提高核心竞争力. 帮助大家寻找分析源码的切入点,在思想上来一次巨大的升华.知其然,并知其所以然.把知识变成自 ...
- Lucene的基本使用
1.了解搜索技术 2.搜索引擎的原理 索引:类似于书的目录 3.实现搜索技术的方式 方式1:数据库搜索 利用SQL语句进行模糊搜索: select * from items where title l ...
- koa2+log4js+sequelize搭建的nodejs服务
主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...
- LTE学习笔记(一)——背景知识
一.标准化组织 无线通信技术的演进离不开一些标准化组织. 1.ITU(International Telecommunication Union) 国际电信联盟,主要任务是制定标准,分配无线频谱资源, ...
- 一、python基本语法元素(温度转换)
#C=(F-32)/1.8 ; F = C * 1.8 + 32 TempStr = input("请输入带有符号的温度值:") if TempStr[-1] in ['f','F ...
- MPP数据库
MPP数据库 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lyc417356935/article/details/45033069 MPP数据库定 ...
- Spring MVC Controller异常处理总结
在项目开发过程中,经常遇到服务被攻击的情况,虽然接口在设计过程中有相当多的安全措施,例如cookie校验.风控.访问熔断等相关技术保证服务的安全性,不过感觉还是有必要收集分析一下这些攻击请求者,以备为 ...
- gentoo 工具命令
查看具体某个命令来源于哪个软件包,使用 equery,比如说查找 lsusb 来源于哪个包,可以用 equery belongs lsusb
- python3 json.dump乱码问题
json.dumps(obj, ensure_ascii=False) ensure_ascii = True,会忽略掉non-ascii字符