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 ...
随机推荐
- Python脚本模拟僵尸进程与孤儿进程
最近一台机器的systemd内存高达30%多,一直不变,后来排查是僵尸进程,什么是僵尸进程呢,只能google,百度等先了解,然后自己总结了一下,虽然这是基础的东西,但是对于我来说就如新大陆一样.花了 ...
- CentOS6.8 配置LVM
LVM是逻辑盘卷管理(Logical Volume Manager)的简称,Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性. LVM ...
- python设置路径值时为什么要输入r
r:代表处理不转义现象 Python中,u表示unicode string,表示使用unicode进行编码,没有u表示byte string,类型是str,在没有声明编码方式时,默认ASCI编码.如果 ...
- Spring生态研习【三】:Spring-kafka
1. 基本信息介绍 基于spring的kafka应用,非常简单即可搭建起来,前提是要有一个kafka的broker集群.我在之前的博文里面已经介绍并搭建了一套broker环境,参考Kafka研究[一] ...
- Wireshark win7 没有找到接口;找不到接口
下载安装winpcap: https://www.winpcap.org/install/default.htm
- VSCode TypeScript开发
下载VSCode VSCode是我使用过最棒的编辑器没有之一. 创建项目 创建目录 首先我们创建一个项目文件夹,比如叫ts_vscode,然后创建一些主要目录,在VSCode中打开项目 目录结构: t ...
- JavaScript常见的代码精简
1.&& callback && callback() 等价于: if(callback){ callback(); } 表达的意思: 先判断 callback 是不是 ...
- Android批量验证渠道、版本号(windows版)
功能:可校验单个或目录下所有apk文件的渠道号.版本号,此为windows版,稍后整理Linux版使用说明:1.copy需要校验的apk文件到VerifyChannelVersion目录下2.双击运行 ...
- Sqluldr2 libclntsh.so报错处理
Sqluldr2 libclntsh.so报错处理 处理报错 [oracle@oracledg tmp]$ ./sqluldr2linux64.bin ./sqluldr2linux64.bin: e ...
- ijkplayer总结
12.ijkplayer的使用过程: 11.ijkpalyer引言: ==== 12.ijkplayer的使用过程: >>举例mac系统编译.so文件: ijkplayer默认是不支持 ...