当前位置:网站首页>I'm late for school
I'm late for school
2022-06-30 09:18:00 【Young white horse】
Unit shortest path problem
Topic link
The following is the explanation of this topic written by a blogger who happened to see it , Personally, I think it's very good !!
A blogger explained in depth
This is the same blog that the blogger spent a long time writing , It can be said that great efforts have been injected , I have written down my understanding of the algorithm
Have a thorough understanding of dijkstra+ Heap optimization code idea
Title Description
Niuniu gets up in the morning and has a look , I slept , Get up and get ready to go to school , There are only two ways for him to go to school , Take the bus and walk , Niuniu goes to school in a straight line , There are a total of on this line n A station , The distance between the stations is equal , There is only one kind of bus at each station ai, Each bus stops only at the corresponding bus stop , The speed of each bus is also different , The first i It takes time for a bus to pass a stop ti, And the bus runs in one direction , Only from left to right , You can walk at will , However, the time it takes Niuniu to walk one stop by himself is T, It happens that Niuniu's home and school are at a certain site , Respectively s and t, How much time does it take to get to school ?
Topic analysis : This is a very good topic , I think the main test sites are under construction , A very ingenious method of drawing , It was meant to clarify my thinking , But I happened to see an explanation , It's a great sight , So learn from bloggers' blogs , The blogger spoke very thoroughly , It can be seen that the blogger is writing a blog carefully , A thoughtful blog is worth caring for .
Let's talk about the method of drawing construction , We just need to build a road between two adjacent points , Others can be transferred
Where every bus can park is one-way , It can be represented as a directed edge in the graph . Be careful. , The directed edge we want to store is not the directed edge from each station to all the stops behind it , It is the directed edge of the adjacent parking station , such as 1 2 2 1 2 1 1 2 1, For the first position 1 For cars , We just need to match it to the fourth position 1 The car builds a one-way edge , There is no need to connect with the sixth position 1 Car construction has a directed edge , Because as long as the directed edges of adjacent parking stations are constructed , It can be built naturally through the transfer of state , This is it. Dijkstra What the algorithm does , So you don't have to do it .
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int pre[maxn], last[maxn], a[maxn], dis[maxn];
const int inf = 0x3f3f3f3f;
#define pii pair<int, int>
int n, m, s, t, T;
vector<pii> g[maxn];
void dijkstra()
{
memset(dis, inf, sizeof(dis));
dis[s] = 0;
priority_queue<pii, vector<pii>, greater<pii> > q;
q.push(make_pair(0, s));// The first position in the queue stores the distance from the source point to this point , The second position records the information of this point
while (!q.empty())
{
pii p = q.top();
q.pop();
int u = p.second;
if (dis[u] < p.first)// If the distance is less than p.first, Then there is no need to update
continue;
for (int i = 0; i < g[u].size(); ++i)// Traverse the adjacency point of the change point
{
int v = g[u][i].second, w = g[u][i].first;
if (dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
q.push(make_pair(dis[v], v));
}
}
}
}
int main()
{
scanf("%d%d%d%d%d", &n, &m, &s, &t, &T);
for (int i = 1; i <= m; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i)// Drawing
{
int x;
scanf("%d", &x);
if (last[x])// If this point is not the starting point of the bus , It's a stop somewhere in the middle , Then save the bus information
g[last[x]].push_back(make_pair(a[x], i));
last[x] = i;// Record the position of adjacent points , Points between nonadjacency can be reached by transfer
// Walk to create two-way edges
g[i].push_back(make_pair(T, i - 1));
g[i - 1].push_back(make_pair(T, i));
}
dijkstra();
printf("%d\n", dis[t]);
}
Self discipline is an attitude towards life , It is a kind of spirit of being a master only after suffering , It is a long-term investment of one person , It is also a kind of , Exchange endless loneliness for the envy of others and the glory of oneself |
---|
A good friend of mine , A few days ago, Baoyan Tsinghua succeeded , My friend , I have been excellent since primary school , Always top a few in age , I have always envied his amazing report card , But with a long time of contact , I also learned that hard work is behind the amazing , And he is extremely disciplined
He's sunny , Very energetic ,, Also very handsome , Is a forever white shirt boy , It is also the first love in the hearts of countless girls , Ha ha ha
Here it is , Bless him , The future is bright !! To rush ~
My favorite this year is it
边栏推荐
- 使用华为性能管理服务,按需配置采样率
- ES6 learning road 5 symbol
- Interviewer: do you understand the principle of recyclerview layout animation?
- Pytorch for former Torch users - Tensors
- [cmake] make command cannot be executed normally
- Row column (vertical and horizontal table) conversion of SQL
- Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
- Deep Learning with Pytorch - autograd
- Esp32 (6): Bluetooth and WiFi functions for function development
- Flutter theme (skin) changes
猜你喜欢
Mmdet line by line deltaxywhbboxcoder
Using appbarlayout to realize secondary ceiling function
C accesses mongodb and performs CRUD operations
So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!
Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)
Evaluation standard for audio signal quality of intelligent speakers
Opencv learning notes -day8 (keyboard typing (waitkey()); Wait for typing) action: triggers some action when the appropriate character is typed using the keyboard)
Esp32 things (3): overview of the overall system design
8.8 heap insertion and deletion
将线程绑定在某个具体的CPU逻辑内核上运行
随机推荐
What are the SQL add / delete / modify queries?
Talking about the difference between kotlin collaboration and thread
Detailed explanation of pipline of mmdetection
Based on svelte3 X desktop UI component library svelte UI
Couldn't load this key (openssh ssh-2 private key (old PEM format))
Linear-gradient()
Handwriting sorter component
Rew acoustic test (VI): signal and measurement
8.8 heap insertion and deletion
About MySQL Boolean and tinyint (1)
About Lombok's @data annotation
Deep Learning with Pytorch- A 60 Minute Blitz
Metasploit practice - SSH brute force cracking process
The elegant combination of walle and Jianbao
Interpretation of orientedrcnn papers
C accesses mongodb and performs CRUD operations
Small program learning path 1 - getting to know small programs
Advanced technology management -- how managers design and build echelons
Pytorch BERT
Get to know handler again