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的更多相关文章

  1. 【离散化】【DFS】Gym - 101617H - Security Badges

    题意:给你一张有向图,每条边有个限制范围,只有权值在限制范围内的人能走这条边,问你权值不超过K的人中,有多少人能从S到T. K很大,因此我们只处理边的范围的上下界这O(m)个权值能否到达,以防万一,还 ...

  2. 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 ...

  3. UVA 10318 Security Panel(DFS剪枝 + 状压 + 思维)题解

    题意:给一个r*c的矩阵开关(初始全打开的),每次按下一个开关都会改变3*3范围内的有*的地方的状态,问你最少几步能让开关全闭上,按升序输出按哪些按钮 思路:每个按钮至多按一下,按按钮的顺序和结果无关 ...

  4. UVA-10318 Security Panel (DFS+剪枝)

    题目大意:求将一个r*c的按钮矩阵由全部为关变成全部为开的最少按扭次数,每按一次开关能作用到的范围不定. 题目分析:开关问题.打眼一看就是BFS+位压缩,但是写出来之后TLE.用DFS不断更新最优解. ...

  5. ERROR [org.apache.hadoop.security.UserGroupInformation] - PriviledgedActionExcep

    换了个环境,出现此异常 016-10-18 23:54:01,334 WARN [org.apache.hadoop.util.NativeCodeLoader] - Unable to load n ...

  6. zoj3811 Untrusted Patrol (dfs)

    2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...

  7. 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 ...

  8. org.apache.hadoop.security.AccessControlException: Permission denied:

    org.apache.hadoop.security.AccessControlException: Permission denied: user=xxj, access=WRITE, inode= ...

  9. Valid Pattern Lock(dfs + 暴力)

    Valid Pattern Lock Time Limit: 2 Seconds      Memory Limit: 65536 KB Pattern lock security is genera ...

随机推荐

  1. 简单的shell脚本练习(一)

    1:求1000一内的偶数和 方法一: #!/bin/bash #用累加实现1000以内的偶数和 sum= ;i<=;i=i+)) do sum=$(($sum+$i)); done echo $ ...

  2. Java面试题 OOAD & UML+XML+SQL+JDBC & Hibernate

    二.OOA/D 与UML 部分:(共6 题:基础2 道,中等难度4 道) 96.UML 是什么?常用的几种图?[基础] 答:UML 是标准建模语言:常用图包括:用例图,静态图(包括类图.对象图和包图) ...

  3. html动态元素点击事件添加

    很多时候,页面的元素是后期异步动态添加在页面上.页面点击事件无效. 非动态的元素直接$().click();便可以直接触发点击事件,而动态元素需要事先注册事件. $(document).on('cli ...

  4. Web jsp开发学习——网上直播聊天室的简单开发

    整个界面为chat.jsp: 如果用户没有登录,就不能进行聊天. 为将发言的句子传到页面上,要设置一个<iframe></iframe>虚拟框架,将allmessage.jsp ...

  5. C++Primer第五版——习题答案详解(九)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...

  6. 涂抹mysql笔记-搭建mysql高可用体系

    mysql的高可用体系<>追求更高稳定性的服务体系 可扩展性:横向扩展(增加节点).纵向扩展(增加节点的硬件配置) 高可用性<>Slave+LVS+Keepalived实现高可 ...

  7. Spring线程池的5个要素

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...

  8. 20165205 2017-2018-2 《Java程序设计》课程总结

    20165205 2017-2018-2<Java程序设计>课程总结 每周作业链接总结 预备作业一: 简述了我认为好的师生关系,展望了未来学习java的日子 预备作业二:总结了C语言的学习 ...

  9. C# Excel添加超链接

    操作当前单元格(关键代码就两行) Range range = (Range)ExSheet.Cells[i + 2, j + 1];                                   ...

  10. django 过滤器,标签

    过滤器: <p>{{ date|date:"Y-m-d" }}</p> {#2018-05-28,date是当前时间#} <p>{{ l|len ...