传送门

题目:简单理解就是,我们需要开车从s点到t点。车上有一个导航,如果当前点为x,则导航会自动为你提供一条从x到t的最短的路线(如果有多条,则随机选一条),每走到下一个点则会实时更新最短路线,当然,如果你按着之前提供的路线走到下一个点,则路线不会更新。题目提供一条确定的路线,如果按着提供的路线走,问导航最多会更新几次,最少会更新几次。

思路:切入点很简单,我们按着路线一个个点走,需要确定走到该点的时候,该点是不是包含在最短路线中,如果包含,是不是唯一的,如果唯一,不更新,否则更新;如果不包含,也会更新。因为我们需要确定从每个点出发走到t的最短距离从而确定导航需不需要更新,所以我们需要反向建边,然后跑一个最短路,求得t到其他点的最短距离,也就得到其他点到t的最短距离。这样,我们只需要枚举路线的当前点,得到路线中下一个点到t的距离,然后按照“红色”更新答案即可。

  1 #include <iostream>
2 #include <algorithm>
3 #include <cstdio>
4 #include <cstring>
5 #include <queue>
6 #include <string>
7 #include <map>
8 #include <set>
9 #include <vector>
10 #define LL long long
11 using namespace std;
12
13 const int N = 2e5 + 10;
14 const int INF = 1e9;
15 vector<int > E[2][N];
16 vector<int > lev[N];
17 int d[2][N], v[N];
18 int n, m;
19
20 void bfs(int s, int t, int pos)
21 {
22 for(int i = 1; i <= n; ++i) d[pos][i] = 1e9;
23 d[pos][s] = 1;
24 queue<int > que;
25 que.push(s);
26
27 while(!que.empty()) {
28 int now = que.front();
29 que.pop();
30
31 for(auto to : E[pos][now]) {
32 if(d[pos][to] > d[pos][now] + 1) {
33 d[pos][to] = d[pos][now] + 1;
34 que.push(to);
35 }
36 }
37 }
38 }
39
40 void solve()
41 {
42 scanf("%d%d", &n, &m);
43 for(int i = 0; i < m; ++i) {
44 int x, y;
45 scanf("%d%d", &x, &y);
46 E[0][x].push_back(y);
47 E[1][y].push_back(x);///反向边
48 }
49
50 int steps;
51 scanf("%d", &steps);
52 for(int i = 1; i <= steps; ++i) { scanf("%d", v + i); }
53
54 bfs(v[1], v[steps], 0);
55 bfs(v[steps], v[1], 1);///反向最短路
56 /*
57 for(int i = 1; i <= n; ++i) {
58 printf("d[%d] = %d\n", i, d[0][i]);
59 }
60 for(int i = 1; i <= n; ++i) {
61 printf("d[%d] = %d\n", i, d[1][i]);
62 }
63
64
65 for(int i = 1; i <= n; ++i) {
66 if(d[1][i] == INF) continue;
67 lev[ d[1][i] ].push_back(i);
68 }
69 */
70 int Min, Max;
71 Min = Max = 0;
72 for(int i = 1; i < steps; ++i) {
73 int now = v[i];
74 int to = v[i + 1];
75 int to_d = d[1][to];
76 int flag = 0;
77 int tot = 0;
78 for(auto other : E[0][now]) {
79 if(to == other) continue;
80 if(to_d > d[1][other]) { ///不是最短路
81 Max++;
82 Min++;
83 flag = 1;
84 break;
85 } else if(to_d == d[1][other]) { tot++; }
86 }
87
88 if(!flag && tot) { Max++; } ///不是唯一的最短路
89 }
90
91 ///printf("Min = %d Max = %d\n", Min, Max);
92 printf("%d %d\n", Min, Max);
93 }
94
95 int main()
96 {
97 solve();
98
99 return 0;
100 }

B. Navigation System【CF 1320】的更多相关文章

  1. 【CF#338D】GCD Table

    [题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...

  2. 【CF#303D】Rotatable Number

    [题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...

  3. 【CF 463F】Escape Through Leaf

    题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...

  4. 【16.50%】【CF 44G】Shooting Gallery

    time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  6. 【CF 585E】 E. Present for Vitalik the Philatelist

    E. Present for Vitalik the Philatelist time limit per test 5 seconds memory limit per test 256 megab ...

  7. 【35.20%】【CF 706D】Vasiliy's Multiset

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 【26.8%】【CF 46D】Parking Lot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. 【31.42%】【CF 714A】Meeting of Old Friends

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

随机推荐

  1. 聊聊 elasticsearch 之分词器配置 (IK+pinyin)

    系统:windows 10 elasticsearch版本:5.6.9 es分词的选择 使用es是考虑服务的性能调优,通过读写分离的方式降低频繁访问数据库的压力,至于分词的选择考虑主要是根据目前比较流 ...

  2. Pytest学习(十一)- 失败重跑插件pytest-rerunfailures的使用

    环境依赖 Python 3.5, 最高 3.8, or PyPy3 pytest 5.0或更高版本 插件安装 pip3 install pytest-rerunfailures -i http://p ...

  3. Happen-Before规则

  4. 基于Docker搭建pypi私有仓库

    一.搭建 1.准备htpasswd.txt文件 该文件内容包含上传包至仓库时验证的用户名和密码 pip install htpasswd htpasswd -sc htpasswd.txt <u ...

  5. 知识解析:C语言函数有一些什么?为你呈现最全函数大全

    大家双节快乐呀~国庆节过去了一半,大家放了几天假呀?玩的开心吗? 如果假日没有其他安排,不要宅在家虚度光阴哦~看看我的文章或者视频学习一些知识吧~   今天为大家分享C语言库函数知识. 以下图片以字母 ...

  6. HEXO & CARDS主题进阶配置

    我想对于建立一个网站而言,第一步要能够做到正常在线访问以及定期产出一定的内容, 其实当网站建立好那一刻,这第一步已经算是完成了,不过我在此基础之上做了些扩展 在默认的card主题之上,我设置了标签.分 ...

  7. shardingsphere与分布式事务

    rt https://blog.csdn.net/l1028386804/article/details/79769043 https://blog.csdn.net/qq_20387013/arti ...

  8. [题解] [USACO05JAN]Muddy Fields G

    题目TP门 题目大意 在一个 \(R×C\) 的矩阵中,每个点有两个状态:草地和泥地.你需要在泥地里铺 \(1×k\) 木块, \(k\) 为任意整数,求最少要多少木块. 思路 两个横向木块不会互相干 ...

  9. docker 部署 mongodb 并且开启远程连接

    mongodb 使用 docker 部署 mongodb 拉取镜像 docker pull mongo 可以查看镜像是否下载成功 docker images | grep mongo 应该会有如下的显 ...

  10. 20191226_rpm命令

    安装rpm包: [root@localhost ~]# rpm -ivh test.rpm rpm查询命令: [root@localhost ~]# rpm -qa | grep mysql mysq ...