Codeforces 1321D Navigation System
题意
有个人要从\(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的更多相关文章
- O - Navigation System CodeForces - 1321D
题目大意:有一个导航系统,会根据你当前的位置,规划到目的地的最短路线,给你一个有向图,和一条行驶路径,问你导航重新规划路径的最大次数和最小次数. 读题的时候题意特别不理解,何为最大次数,何为最小次数? ...
- Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) D. Navigation System(有向图,BFS,最短路)
题意: n 点 m 边有向图,给出行走路径,求行走途中到路径终点最短路变化次数的最小值和最大值 . 思路 : 逆向广搜,正向模拟. #include <bits/stdc++.h> usi ...
- Sherpa | Complete Navigation System 介绍与教材
这里的夏尔巴人在iPad上使用的一种新的视频. 正如其名称所暗示的,夏尔巴人是所有您的导航需求的整体解决方案. 夏尔巴人带来了每个接口的三个主要的导航元素结合在一起 - 导航栏,侧边栏和页脚粘. 每一 ...
- (水题)Codeforces - 4C - Registration system
https://codeforces.com/problemset/problem/4/C 用来哈希的一道题目,用map也可以强行过,但是性能慢了6倍,说明是在字符串比较的时候花费了接近6倍的时间. ...
- 【codeforces 22C】 System Administrator
[题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...
- codeforces C. Booking System
题意:有n组客人,分别告诉每一组的个数和花费,然后给你餐厅内k个桌子,每个桌子的最大容纳人数,如何安排使得餐厅最大收益并且容纳人数尽可能大: 思路:贪心,对花费排序,然后对每一组客人找桌子就可以. # ...
- Codeforces 458A Golden System
经过计算两个字符串的大小对比 主要q^2=q+1 明明是斐波那契数 100000位肯定超LL 我在每一位仅仅取到两个以内 竟然ac了 #include<bits/stdc++.h> usi ...
- B. Navigation System【CF 1320】
传送门 题目:简单理解就是,我们需要开车从s点到t点.车上有一个导航,如果当前点为x,则导航会自动为你提供一条从x到t的最短的路线(如果有多条,则随机选一条),每走到下一个点则会实时更新最短路线,当然 ...
- Codeforces Round #625 (Div. 2)
Contest Info Practice Link Solved A B C D E F 4/6 O O Ø Ø O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Sol ...
随机推荐
- python 版本 jaeger-client 导入失败 jaeger-client-python
环境为: OS: ubuntu18.04 Python: 3.6 问题原因: 尝试使用 jaeger-client-python,官方给出的示例(https://github.com/jaegertr ...
- Go 中的动态作用域变量
这是一个 API 设计的思想实验,它从典型的 Go 单元测试惯用形式开始: func TestOpenFile(t *testing.T) { f, err := os.Open("notf ...
- dom4j基本操作
DOM4J与利用DOM.SAX.JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异.功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml. ...
- C#LeetCode刷题之#56-合并区间(Merge Intervals)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3676 访问. 给出一个区间的集合,请合并所有重叠的区间. 输入: ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题之#28-实现strStr()(Implement strStr())
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3895 访问. 实现 strStr() 函数. 给定一个 hays ...
- K8s集群verification error" while trying to verify candidate authority certificate "kubernetes"
问题内容 because of "crypto/rsa: verification error" while trying to verify candidate authorit ...
- GUAVA-ListenableFuture实现回调
随着软件开发的不断进步,在实际的开发应用中,可能一次请求需要查询若干次数据库或者调用若干次第三方,按照传统的串行执行的话,会大大增加响应时间,无法满足业务需求,更无法满足用户迫切需要响应迅速的愿望.对 ...
- SpringMVC实现客户端跳转
之前无论是/index跳转到index.jsp 还是/addProduct 跳转到showProduct.jsp,都是服务端跳转. 这一篇练习如何进行客户端跳转 @ 目录 修改IndexControl ...
- Nordic 52840-Timer定时器学习问题(一)
今天在ble_app_blinky例程中移植定时器驱动,在编译过程中报出了两个错误,在此记录一下. 1. 在nRF_Dreivers中添加nrfx_timer.c文件 选中“nRF_Dreivers ...