B. Navigation System【CF 1320】



题目:简单理解就是,我们需要开车从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】的更多相关文章
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【CF#303D】Rotatable Number
[题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...
- 【CF 463F】Escape Through Leaf
题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...
- 【16.50%】【CF 44G】Shooting Gallery
time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【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 ...
- 【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 ...
- 【35.20%】【CF 706D】Vasiliy's Multiset
time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【26.8%】【CF 46D】Parking Lot
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【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 ...
随机推荐
- 巧妙使用MathType快速编写数学函数公式
在我们日常的工作与学习中,你是否也会遇到过无法在电脑中编写数学函数公式的情况呢? 简单的数学函数公式或许经过我们不懈的努力也可以成功的编写,不过这会耽误我们大把的时间. 想象一下,假如你的老板急着催你 ...
- guitar pro系列教程(十二):如何设置Guitar Pro的不完全小节
当我们新建一个GTP谱的时候,我们肯定是要用到节拍,是的,一个乐谱节拍设置的好不好,将直接影响你的乐谱效果好不好,设置节拍的步骤我们之前也有讨论过,今天主要跟大家讲的便是不完全小节. 不完全小节顾名思 ...
- LeetCode周赛#212
1631. 最小体力消耗路径 #并查集 #最短路径 题目链接 题意 给定一二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 \((row ...
- String.Split()函数 非原创
我们在上次学习到了 String.Join函数(http://blog.csdn.net/zhvsby/archive/2008/11/28/3404704.aspx),其中用到了String.SPl ...
- HTTP系列(一)URI、URL、URN的区别
1.URI.URL.URN关系图 1)URI Uniform Resource Identifier 统一资源标识符 每个web服务器资源都有一个名字,服务器资源名被统称为统一资源标识符:URI就像 ...
- PyQt(Python+Qt)学习随笔:model/view架构中类QStandardItemModel的使用方法
老猿Python博文目录 老猿Python博客地址 一.概述 QStandardItemModel是QAbstractItemModel的派生类,用于在Model/View架构中存储自定义数据的通用模 ...
- Docker下Python Flask+Redis+MySQL+RQ队列简单配置
本篇博文主要讲解Docker下使用RQ队列的通信配置,主要是网上的部分文章写的不太清楚,特写一篇 作者使用docker-compose.yml文件调度各部分文件Dockerfile,起初是这样写的 v ...
- C语言网络编程(Linux && Windows)(1)
和朋友一起做课程设计,同时学习C语言的网络编程,以前写的都是python网络编程,但python很多的库都是封装好的,大部分人在使用的时候不会去了解底层的实现,这样对长远的学习不太好,也改正自己这方面 ...
- 在 GitHub 玩硬件——GitHub 热点速览 Vol.49
作者:HelloGitHub-小鱼干 本周的 GitHub Trending 可以说是非常之硬核,天才少年稚晖君的 2 个硬件变装项目荣登热点榜,看完将充电宝改装为显示器的视频,搭配 HDMI-PI ...
- 初识Flask——基于python的web框架
参考教程链接: https://dormousehole.readthedocs.io/en/latest/ (主要)https://www.w3cschool.cn/flask/ 目录: 1.写了一 ...