当前位置:网站首页>"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;
}
边栏推荐
- Solve the width overflow of rich text pictures such as uniapp
- 优化Hypermesh脚本性能的几点建议
- 遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
- El input limit can only input the specified number
- 李宏毅《机器学习》丨4. Deep Learning(深度学习)
- 小程序中的分页查询
- Automatic conversion and cast
- Implementation of skip table
- 加速投资的小红书,“病急乱投医”?
- LwIP development | socket | DNS domain name resolution
猜你喜欢

SCI scientific paper writing Growth Camp (full version)

I'll show you a little chat! Summary of single merchant function modules

Sort 4-heap sort and massive TOPK problem

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

HM二次开发 - Data Names及其使用

Stm32cube infrared remote control: input capture

在abaqus中使用PyQt设计GUI

LabVIEW Linx toolkit controls Arduino equipment (expansion-1)

A good start

软件问题修复跟踪系统实战开发教程(上篇)
随机推荐
资本「断供」两年,我只能把公司卖了
I'll show you a little chat! Summary of single merchant function modules
The epidemic dividend disappeared, and the "home fitness" foam dissipated
curl无输出返回空白或者null问题解决
mysql查询 limit 1000,10 和limit 10 速度一样快吗?如果我要分页,我该怎么办?
QT packaging
Ffmpeg get the first frame
I can only sell the company after the capital has been "cut off" for two years
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
ANSA二次开发 - 抽中面的两种方法
Practical development tutorial of software problem repair tracking system (Part 1)
“蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
软考 系统架构设计师 简明教程 | 软件调试
Use py to automatically generate weekly reports based on log records
El input limit can only input the specified number
PHP about problems such as memory overflow timeout caused by large amount of data exporting or traversing data
排序3-选择排序与归并排序(递归实现+非递归实现)
A program for judging the result of cyclic input