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 ...
随机推荐
- 前端(web)知识-html
前端由三部分组成: HTML(标签)--CSS(美化,修饰)--JS(执行指令) HTML(超文本标记语言,Hypertext Markup Language):是一种用于创建网页的标记语言. 本质上 ...
- SRX_Test_2_key
转载自 Livedream YBT1396 #include<iostream> #include<map> #include<queue> #include< ...
- 抓包工具fiddler使用-初级
参考 https://kb.cnblogs.com/page/130367/#introduce
- LeetCode周赛#204 题解
1566. 重复至少 K 次且长度为 M 的模式 #模拟 题目链接 题意 给定正整数数组 arr,请你找出一个长度为 m 且在数组中至少重复 k 次的模式. 模式 是由一个或多个值组成的子数组(连续的 ...
- 求1-1e11内的素数个数(HDU 5901 Count primes )
参考链接:https://blog.csdn.net/Dylan_Frank/article/details/54428481 #include <bits/stdc++.h> #defi ...
- C语言讲义——冒泡排序(bubble sort)
冒泡排序三步走: 循环 交换 回一手 一个数和其它数比较(循环) 每个数都要做这种比较(再一层循环) 准备工作 #include <stdio.h> void sort(int arr[] ...
- Java反射——读取XML文件,创建对象
读取XML文件,创建对象 config.xml <?xml version="1.0" encoding="UTF-8"?> <beans&g ...
- C++基础知识篇:C++ 基本语法
C++ 基本语法 C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互.现在让我们简要地看一下什么是类.对象,方法.即时变量. 对象 - 对象具有状态和行为.例如:一只狗的状态 - 颜 ...
- rocketmq的吞吐量为什么小于kafka
转载: https://www.jianshu.com/p/c474ca9f9430
- C#中的WinForm问题——使用滚动条时页面闪烁及重影问题
当使用鼠标进行滚动查看页面时,由于页面会频繁刷新,如果页面中控件较多会导致页面出现闪烁.重影等问题,如下图所示: 在网上搜索过该问题,大部分都说使用双缓冲可以解决此类问题,即通过设置DoubleBuf ...