题意

有个人要从\(s\)走到\(t\),经过的路径给定。导航系统每次会显示当前节点到\(t\)的最短路,有多条就显示其中之一。这个人如果按照导航走,那么啥都没变。如果没有按导航走导航就会重新导航。问重新导航的最小和最大次数。

解题思路

建反图,在反图上以\(t\)为源跑dijkstra最短路。

在原图上dfs

  • 若下一个节点到\(t\)的距离是所有邻接节点中最短的

    • 如果到\(t\)最短的节点只有一个,那么什么都不变。
    • 如果不止一个,那么最大次数加1。
  • 若下一个节点到\(t\)的距离不是所有邻接节点中最短的,那么最大次数和最小次数都加1。

AC代码

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
typedef pair<int,int> pi; #define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define endl '\n' const double PI=acos(-1.0); namespace IO{
bool REOF = 1;//为0表示文件结尾
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && REOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (REOF = 0, EOF) : *p1++;
} template<class T>
inline bool read(T &x) {
if(!REOF)return false;
char c = nc();bool f = 0; x = 0;
while (c<'0' || c>'9')c == '-' && (f = 1), c = nc();
while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
if(f)x=-x;
return true;
} template<typename T, typename... T2>
inline bool read(T &x, T2 &... rest) {
if(!REOF)return false;
read(x);
return read(rest...);
} inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')); }
// inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || c==' '; } inline bool read_str(char *a) {
if(!REOF)return false;
while ((*a = nc()) && need(*a) && REOF)++a; *a = '\0';
return true;
} inline bool read_dbl(double &x){
if(!REOF)return false;
bool f = 0; char ch = nc(); x = 0;
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=nc();}
while(ch>='0'&&ch<='9'){x=x*10.0+(ch^48);ch=nc();}
if(ch == '.') {
double tmp = 1; ch = nc();
while(ch>='0'&&ch<='9'){tmp=tmp/10.0;x=x+tmp*(ch^48);ch=nc();}
}
if(f)x=-x;
return true;
} template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;
cerr<<'='<<h<<','<<' '; _dbg(sdbg+1, a...);
} template<class T> ostream &operator<<(ostream& os, vector<T> V) {
os << "["; for (auto vv : V) os << vv << ","; return os << "]";
} template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.st << "," << P.nd << ")";
} #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
} using namespace IO;
const int maxn=2e5+5;
const int maxv=2e5+5;
const int mod=998244353; // 998244353 1e9+7
const int INF=1e9+7; // 1e9+7 0x3f3f3f3f 0x3f3f3f3f3f3f3f3f
const double eps=1e-12; int dx[4]={0,1,0,-1};
//int dx[8]={1,0,-1,1,-1,1,0,-1};
int dy[4]={1,0,-1,0};
//int dy[8]={1,1,1,0,0,-1,-1,-1}; // #define ls (x<<1)
// #define rs (x<<1|1)
// #define mid ((l+r)>>1)
// #define lson ls,l,mid
// #define rson rs,mid+1,r /**
* ********** Backlight **********
* 仔细读题
* 注意边界条件
* 记得注释输入流重定向
* 没有思路就试试逆向思维
* 加油,奥利给
*/ struct Graph{
int tot,head[maxn];
struct Edge{
int v,nxt;
}e[maxn<<1];
void init(){
tot=1;
memset(head,0,sizeof(head));
}
void addedge(int u,int v){
e[tot].v=v;e[tot].nxt=head[u];
head[u]=tot++; // e[tot].v=u;e[tot].nxt=head[v];
// head[v]=tot++;
}
}G1,G2; int n,m,k,p[maxn],dis[maxn];
int mi,ma;
void dijkstra(int S){
priority_queue<pi, vector<pi>, greater<pi> > q;
for (int i = 1; i <= n; i++) dis[i] = INF;
dis[S] = 0; q.push(make_pair(0, S));
while (!q.empty()){
pi p = q.top(); q.pop();
if (dis[p.second] != p.first) continue;
for (int i = G2.head[p.second]; i; i=G2.e[i].nxt){
int v = G2.e[i].v, w=1;
if (dis[v] > dis[p.second] + w){
dis[v] = dis[p.second] + w;
q.push(make_pair(dis[v], v));
}
}
}
} void dfs(int id){
if(id==k){ return;
}
int u=p[id],w=p[id+1];
int MIN=INF,cnt=0;
for(int i=G1.head[u];i;i=G1.e[i].nxt){
int v=G1.e[i].v;
if(dis[v]<MIN){
MIN=dis[v];
cnt=0;
}
if(dis[v]==MIN)cnt++;
} if(dis[w]==MIN){
if(cnt>1)ma++;
}
if(dis[w]>MIN){
mi++;
ma++;
}
dfs(id+1);
} void solve(){
read(n,m);
int u,v;
G1.init(); G2.init();
for(int i=1;i<=m;i++){
read(u,v);
G1.addedge(u,v);
G2.addedge(v,u);
}
read(k);
for(int i=1;i<=k;i++)read(p[i]); dijkstra(p[k]); dfs(1);
printf("%d %d\n",mi,ma);
} int main()
{
// freopen("in.txt","r",stdin);
// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int _T; read(_T); for(int _=1;_<=_T;_++)solve();
// while(read(n))solve();
solve();
return 0;
}

Codeforces 1321D Navigation System的更多相关文章

  1. O - Navigation System CodeForces - 1321D

    题目大意:有一个导航系统,会根据你当前的位置,规划到目的地的最短路线,给你一个有向图,和一条行驶路径,问你导航重新规划路径的最大次数和最小次数. 读题的时候题意特别不理解,何为最大次数,何为最小次数? ...

  2. Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) D. Navigation System(有向图,BFS,最短路)

    题意: n 点 m 边有向图,给出行走路径,求行走途中到路径终点最短路变化次数的最小值和最大值 . 思路 : 逆向广搜,正向模拟. #include <bits/stdc++.h> usi ...

  3. Sherpa | Complete Navigation System 介绍与教材

    这里的夏尔巴人在iPad上使用的一种新的视频. 正如其名称所暗示的,夏尔巴人是所有您的导航需求的整体解决方案. 夏尔巴人带来了每个接口的三个主要的导航元素结合在一起 - 导航栏,侧边栏和页脚粘. 每一 ...

  4. (水题)Codeforces - 4C - Registration system

    https://codeforces.com/problemset/problem/4/C 用来哈希的一道题目,用map也可以强行过,但是性能慢了6倍,说明是在字符串比较的时候花费了接近6倍的时间. ...

  5. 【codeforces 22C】 System Administrator

    [题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...

  6. codeforces C. Booking System

    题意:有n组客人,分别告诉每一组的个数和花费,然后给你餐厅内k个桌子,每个桌子的最大容纳人数,如何安排使得餐厅最大收益并且容纳人数尽可能大: 思路:贪心,对花费排序,然后对每一组客人找桌子就可以. # ...

  7. Codeforces 458A Golden System

    经过计算两个字符串的大小对比 主要q^2=q+1 明明是斐波那契数 100000位肯定超LL 我在每一位仅仅取到两个以内 竟然ac了 #include<bits/stdc++.h> usi ...

  8. B. Navigation System【CF 1320】

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

  9. Codeforces Round #625 (Div. 2)

    Contest Info Practice Link Solved A B C D E F 4/6 O O Ø  Ø     O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Sol ...

随机推荐

  1. python 爬虫刷访问量

    import urllib.requestimport time # 使用build_opener()是为了让python程序模仿浏览器进行访问opener = urllib.request.buil ...

  2. XCTF-WEB-新手练习区(5-8)笔记

    5:disabled_button X老师今天上课讲了前端知识,然后给了大家一个不能按的按钮,小宁惊奇地发现这个按钮按不下去,到底怎么才能按下去呢? 删除disable="" 字段 ...

  3. Springboot开启事务的支持

    主要分为两步 步骤一.在main方法加上@EnableTransactionManagement注解: @SpringBootApplication @EnableTransactionManagem ...

  4. 【Spring Security】1.快速入门

    1 导入Spring Security的相关依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  5. asp.netcore3.1 将服务器配置为需要证书

    运行 asp.netcore 3.1应用程序时,弹出证书选择框. 将服务器配置为需要证书(Kestrel),在Program.cs中,按如下所示配置 Kestrel: public static vo ...

  6. JavaScript学习系列博客_18_JavaScript中的匿名函数

    匿名函数 - 用函数声明的方式创建一个函数时,不加函数名称. function sum(){ console.log("我是函数sum")} - 不加名称,这样写浏览器是会报错的. ...

  7. govendor 使用

    govendor是go语言依赖管理工具,推荐使用 https://github.com/kardianos/govendor 这个版本. go get -u -v github.com/kardian ...

  8. 如何下载gitbub中的单个文件

    1.进入Github文件夹,打开对应文件: 2.右键单击Raw,然后目标另存为即可.

  9. SQL语句组合查询 UNION

    1.使用UNION UNION 可以涉及编写多条SELECT语句,首先看看单条语句 第一条SELECT语句把Illinois,Indiana,Michigan等州的缩写传递给IN子句,检索出这些州的所 ...

  10. Python脚本实现在cmd执行相关命令

    通过Python脚本实现,在cmd命令执行文件的cp(复制).rm(删除).rename(重命名).move(文件移动).mkdir(创建目录) cmd执行命令格式:python xxx.py 用户名 ...