CSES Flight Discount Problem | Dijkstra's Algorithm | Complete Walkthrough in C++

  Рет қаралды 180

Code With U-DAY

Code With U-DAY

Күн бұрын

Пікірлер: 5
@U-DAY
@U-DAY 4 ай бұрын
The core logic remains the same, but eliminating constant time operations is necessary to fix the TLE. I’m sharing this code to provide better readability and clarity of my approach. Feel free to review it! #include using namespace std; #define ll long long vector dijkstra_algo(ll st, vectoradj[], ll n) { priority_queuepq; vectorvis(n, LONG_MAX); pq.push({0, st}); vis[st] = 0; while(!pq.empty()) { auto it = pq.top(); pq.pop(); ll x = it.first; ll y = it.second; for(auto i: adj[y]) { ll curr = x + i.second; if(curr < vis[i.first]) { vis[i.first] = curr; pq.push({curr, i.first}); } } } return vis; } int main() { ll m, n; cin>>n>>m; vectoradj[n]; vectorradj[n]; vectoredges; for(int i=0; i>x>>y>>z; x--, y--; adj[x].push_back({y, z}); radj[y].push_back({x, z}); edges.push_back({x, y, z}); } vectorvis1 = dijkstra_algo(0, adj, n); vectorvis2 = dijkstra_algo(n-1, radj, n); ll ans = 1e18; for(auto it: edges) { ll curr = vis1[it[0]] + vis2[it[1]] + it[2]/2; ans = min(ans, curr); } cout
@mahmoudabdellah5332
@mahmoudabdellah5332 7 күн бұрын
Good Work
@4rt15t
@4rt15t 2 ай бұрын
thank you!!!
@deshdeepakkant524
@deshdeepakkant524 3 ай бұрын
nice explaintion
@varshith3740
@varshith3740 4 ай бұрын
very good explanation bro
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
Dijkstra's Algorithm:  Another example
8:41
barngrader
Рет қаралды 801 М.
[CSES][Sorting and Searching] Reading Books
8:43
NeatlyStructured
Рет қаралды 3,8 М.
Path Planning - A* (A-Star)
31:18
javidx9
Рет қаралды 163 М.
Preparation and Strategy | Bridge | Power Programmer | Infosys
18:20
Dijkstra's Algorithm with example of undirected graph
12:31
Beena Ballal
Рет қаралды 347 М.
Dijkstra's Algorithm vs. A* Search vs. Concurrent Dijkstra's Algorithm
4:46
Pathfinding algorithm comparison: Dijkstra's vs. A* (A-Star)
2:39
Anthony Madorsky
Рет қаралды 146 М.
Flight Discount | CSES PROBLEM SET | DIJKSTRA ALGORITHM
19:38
Hitesh Tripathi
Рет қаралды 4,3 М.