当前位置:网站首页>"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;
}
边栏推荐
- 为什么学编程的人大多数都去了深圳和北京?
- 动态规划 --- 数位统计DP
- Rosen's QT journey 102 listmodel
- 在vs code上配置Hypermesh二次开发环境
- Notes on October 22, 2021
- Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
- The video Number finds the golden key, and Tiktok imitates the latecomers
- Detailed explanation of QT qstring
- php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
- 资本「断供」两年,我只能把公司卖了
猜你喜欢

The little red book of accelerating investment, "rush to medical treatment"?

5 亿用户,比微信还早四年……这个运营了 15 年的 APP 即将永久停服

laravel

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

laravel

c语言编程当中两个!!的作用

Zhengda cup hacker marathon data analysis competition

ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境

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

关于标准IO缓冲区的问题
随机推荐
队列的介绍与实现(详解)
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
排序1-插入排序与希尔排序
IT远程运维是什么意思?远程运维软件哪个好?
PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
PHP 图片上传
c语言编程当中两个!!的作用
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
大地坐标系转换火星坐标系
Li Hongyi, machine learning 5. Tips for neural network design
Record doc
李宏毅《机器学习》丨5. Tips for neural network design(神经网络设计技巧)
PHP image upload
A program for judging the result of cyclic input
laravel
flashfxp 530 User cannot log in. ftp
php关于数据量大导出数据或者遍历数据导致内存溢出超时等问题
排序4-堆排序与海量TopK问题
关于MIT6.828_HW9_barriers xv6 homework9的一些问题
在vs code上配置Hypermesh二次开发环境