传送门

题目:简单理解就是,我们需要开车从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. nginx学习http_access_module模块

    location ~ ^/1.html { root /opt/app/code; deny XXXXX; #这个ip不能访问1.html allow all; #其他的可以访问这个页面1.html ...

  2. Mybatis【2.2】-- Mybatis关于创建SqlSession源码分析的几点疑问?

    代码直接放在Github仓库[https://github.com/Damaer/Mybatis-Learning ],可直接运行,就不占篇幅了. 目录 1.为什么我们使用SQLSessionFact ...

  3. C++基础知识篇:C++ 常量

    常量是固定值,在程序执行期间不会改变.这些固定的值,又叫做字面量. 常量可以是任何的基本数据类型,可分为整型数字.浮点数字.字符.字符串和布尔值. 常量就像是常规的变量,只不过常量的值在定义后不能进行 ...

  4. 编程C语言进阶篇——自定义数据类型:共同体

    什么是"自定义数据类型"?顾名思义,就是用户可以随时在程序中自行定义新的数据类型.自定义数据类型时需要设置数据类型的名称及其成员.数据类型成员各属性的设置方法等同于变量设置时相应属 ...

  5. Appium上下文和H5测试(二)

    坚持原创输出,点击蓝字关注我吧 作者:清菡 博客:oschina.云+社区.知乎等各大平台都有. 文章总览图 一.往期回顾 loc='new UiSelector().text("全程班&q ...

  6. vue获取微博授权URL

    1.在Vue**页面加载时动态发送请求获取微博授权url 1.1 在 components\common\lab_header.vue 中写oauth动态获取微 博授权URL // 获取微博登录地址 ...

  7. Spring Boot 中使用 Spring Security, OAuth2 跨域问题 (自己挖的坑)

    使用 Spring Boot 开发 API 使用 Spring Security + OAuth2 + JWT 鉴权,已经在 Controller 配置允许跨域: @RestController @C ...

  8. 通过Dbeaver创建表格的时候,设置主键

    通过Dbeaver创建表格的时候,设置主键 Dbeaver介绍: ​ 这是一个开源的数据库连接工具,你需要安装jre才可以使用这个软件 在使用Dbeaver创建表的时候,会发现,不能直观地设置主键 这 ...

  9. PyQt(Python+Qt)学习随笔:QTreeView树形视图的expandsOnDoubleClick属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的expandsOnDoubleClick属性用于控制鼠标双击是否展开或折 ...

  10. 基于CefSharp开发(六)浏览器网页缩放

    一.网页缩放分析 缩放入口 1.Ctrl + 鼠标滑轮缩放 2.菜单中缩放子菜单缩放 3.搜索框中网页缩放按钮缩放 缩放属性及命令 ChromiumWebBrowser 提供了缩放量值.缩放级别.放大 ...