当前位置:网站首页>"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;
}
边栏推荐
- Redis series 4: sentinel (sentinel mode) with high availability
- 关于web对接针式打印机问题,Lodop使用
- LabVIEW Linx toolkit controls Arduino equipment (expansion-1)
- Sort 4-heap sort and massive TOPK problem
- MySQL view event status statements and modification methods
- Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
- Notes on October 22, 2021
- Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
- 优化Hypermesh脚本性能的几点建议
- 2021-04-02
猜你喜欢

Record doc

Rosen's QT journey 101 models and views in QT quick

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

IT远程运维是什么意思?远程运维软件哪个好?

Image semantic segmentation practice: tensorflow deeplobv3+ train your own dataset

A good start

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

队列的介绍与实现(详解)

mysql 查看事件状态语句和修改办法

nowcode-学会删除链表中重复元素两题(详解)
随机推荐
redis源码优化--绑核
Writing of factorial
Rosen's QT journey 101 models and views in QT quick
深入理解Istio流量管理的熔断配置
Paging query in applet
Brief tutorial for soft exam system architecture designer | software debugging
Qt学习第一天
Thoughts on solving the pop-up of malicious computer advertisements
2021-04-02
The epidemic dividend disappeared, and the "home fitness" foam dissipated
Qt学习之Qt Designer(设计师)
QT QString详解
5 亿用户,比微信还早四年……这个运营了 15 年的 APP 即将永久停服
PHP 图片上传
“蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
食品安全 | 这两类瓜果宜改善便秘 孕妇人群尤其建议
Automatic conversion and cast
信号屏蔽与处理
HyperMesh运行脚本文件的几种方法
nowcode-学会删除链表中重复元素两题(详解)