当前位置:网站首页>"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 j.journey 0-1 shortest path
2022-07-28 16:32:00 【HeartFireY】
J.Journey
Topic analysis
There are several intersections in a given city , You need to wait for the red light to turn right , Go straight 、 Both left turns and U-turns are required , Please wait for the red light at least several times from the beginning to the end .
You can think of each path as a point , The situation of intersection disposal is even , Edge right assignment 0 , 1 0,1 0,1, Turn into 0 − 1 0-1 0−1 Shortest path problem , And then run straight d i j k s t r a dijkstra dijkstra that will do .
Code
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 4e6 + 10;
#define pii pair<int, int>
#define mkp make_pair
#define fir first
#define sec second
int n = 0, cnt = 0;
int s1, s2, t1, t2;
vector<pii> g[N];
int dis[N];
struct node{
int fa, to, res, id;
const bool operator< (const node &x) const {
return res > x.res; }
};
priority_queue<node> pq;
void dijkstra(){
for(int i = 0; i < 4; i++){
auto &[v, k] = g[s1][i];
if(v == s2) pq.emplace(node{
s1, s2, 0, k});
}
while(pq.size()){
auto now = pq.top(); pq.pop();
if(now.to == 0 || dis[now.id] != -1) continue;
dis[now.id] = now.res;
for(int i = 0; i < 4; i++){
auto &[to, k] = g[now.to][i];
if(to == now.fa) pq.emplace(node{
now.to, g[now.to][(i + 1) % 4].fir, now.res, g[now.to][(i + 1) % 4].sec});
else pq.emplace(node{
now.to, g[now.to][(i + 1) % 4].fir, now.res + 1, g[now.to][(i + 1) % 4].sec});
}
}
}
void solve(){
cin >> n;
memset(dis, -1, sizeof(dis));
for(int i = 1; i <= n; i++){
for(int j = 0; j < 4; j++){
int v; cin >> v;
g[i].emplace_back(mkp(v, ++cnt));
}
}
cin >> s1 >> s2 >> t1 >> t2;
dijkstra();
for(int i = 0; i < 4; i++){
auto &[to, k] = g[t1][i];
if(to == t2) cout << dis[k] << endl;
}
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
- 栈的介绍与实现(详解)
- 队列的介绍与实现(详解)
- Practical development tutorial of software problem repair tracking system (Part 1)
- Qt学习之信号和槽机制
- 后台弹出layer提示
- 配置web服务器步骤详细记录(多有借鉴)
- HDU1847解题思路
- 我在上海偶遇数字凤凰#坐标徐汇美罗城
- R language uses ggpairs function of ggally package to visualize pairwise relationship graph of grouping multivariable, set alpha parameter to change image transparency, diagonal continuous variable de
- Dynamic programming -- digital statistics DP
猜你喜欢

QT packaging

Introduction and implementation of queue (detailed explanation)

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

魏建军骑宝马也追不上李书福

我在上海偶遇数字凤凰#坐标徐汇美罗城

Redis series 4: sentinel (sentinel mode) with high availability

Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi

关于标准IO缓冲区的问题

MySQL view event status statements and modification methods

HyperMesh自动保存(增强版)插件使用说明
随机推荐
LeetCode-学会复杂带随机指针链表的题(详解)
小程序中的分页查询
排序5-计数排序
Brief tutorial for soft exam system architecture designer | software debugging
PHP获取小程序码,小程序带参数跳转
ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境
Huada chip hc32f4a0 realizes RS485 communication DMA transceiver
The epidemic dividend disappeared, and the "home fitness" foam dissipated
“蔚来杯“2022牛客暑期多校训练营3 J.Journey 0-1最短路
李宏毅《机器学习》丨4. Deep Learning(深度学习)
mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
资本「断供」两年,我只能把公司卖了
HyperMesh自动保存(增强版)插件使用说明
在vs code上配置Hypermesh二次开发环境
重置grafana登录密码为默认密码
“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
关于标准IO缓冲区的问题
Stm32cube infrared remote control: input capture