传送门

题目:简单理解就是,我们需要开车从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. FL Studio中的音频剪辑功能讲解

    音频剪辑,是FL Studio中的一个特色功能,音频剪辑的目的是保持在播放列表中显示和触发的音频,可以根据需要对它们进行切片和排列.但需要注意的是音频剪辑这个功能在FL Studio的基础版(果味版) ...

  2. Apifox接口测试管理工具

    今天发现开发使用了一款新的接口测试工具,一眼看上去比较清爽,主要全中文界面对比postman的全英文,简直友好太多了. 后续又被业界大佬虫师推荐,于是去官网简单了解了一下,Apifox = Postm ...

  3. 痞子衡嵌入式:探析开启CRC完整性校验的IAR工程生成.out和.bin文件先后顺序

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是开启CRC完整性校验的IAR工程生成.out和.bin文件先后顺序问题. 痞子衡之前写了一篇 <在IAR开发环境下为工程开启CRC ...

  4. Crossing River 题解(贪心)

    题目链接 题目大意 t组数据(t<=20) 给你n个人(n<=1000)过河,每个人都有权值,一条船,每次船最多运2个人,每次的花费为两个人的较大花费 求所有人都过河需要的最小花费 题目思 ...

  5. 深入浅出之mysql索引--上

    当着小萌新之际,最近工作中遇到了mysql优化的相关问题,然后既然提到了优化,很多像我这样的小萌新不容置喙,肯定张口就是 建立索引 之类的. 那么说到底,索引到底是什么,它是怎么工作的?接下来就让我和 ...

  6. HEXO & CARDS主题进阶配置

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

  7. rsync单项同步

    配置rsync+inotify实时单向同步 定期同步的缺点: 执行备份的时间固定,延期明显,实时性差 当同步源长期不变化时,密集的定期任务是不必要的(浪费资源) 实时同步的优点: 一旦同步源出现变化, ...

  8. oracle sql%notfound

    SQL%NOTFOUND 是一个布尔值.与最近的sql语句(update,insert,delete,select)发生交互,当最近的一条sql语句没有涉及任何行的时候,则返回true.否则返回fal ...

  9. 图像处理gamma修正(伽马γ校正)的原理和实现算法

    ☞ ░ 前往老猿Python博文目录 ░ 本文转自博客园:淇淇宝贝的文章<图像处理之gamma校正>,原文链接:https://www.cnblogs.com/qiqibaby/p/532 ...

  10. 第3.10节 Python强大的字符串格式化新功能:使用format字符串格式化

    一.    引言 前面两节介绍的字符串格式化方法,都有其本身对应的缺陷,老猿不建议大家使用,之所以详细介绍主要是考虑历史代码的兼容性,方便大家理解前人留下的代码.老猿推荐大家新编码时使用format方 ...