From 6ffcfb17f058217f1353cbfd6fa3e8e9cc9681a4 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 20 May 2023 20:32:38 +0300 Subject: [DAAP] Solved an exercise from olimpiici --- .../Practicum/Sofiaopen-spring-2023_lights.cpp | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Year 2/Design and analysis of algorithms/Practicum/Sofiaopen-spring-2023_lights.cpp diff --git a/Year 2/Design and analysis of algorithms/Practicum/Sofiaopen-spring-2023_lights.cpp b/Year 2/Design and analysis of algorithms/Practicum/Sofiaopen-spring-2023_lights.cpp new file mode 100644 index 0000000..686319a --- /dev/null +++ b/Year 2/Design and analysis of algorithms/Practicum/Sofiaopen-spring-2023_lights.cpp @@ -0,0 +1,53 @@ + +/* https://arena.olimpiici.com/#/catalog/756/problem/101958 */ + +#include +#include +#include +#include +using namespace std; + +struct Path { + unsigned long long b; + unsigned long long s; + unsigned long long g; + unsigned long long r; +}; + +int main() { + unsigned long long N, M, S; + cin >> N >> M >> S; + vector> crossroads(N+1); + for (long long i = 0; i < M; i++) { + unsigned long long ai, bi, si, gi, ri; + cin >> ai >> bi >> si >> gi >> ri; + crossroads[ai].push_back({ bi, si, gi, ri }); + crossroads[bi].push_back({ ai, si, gi, ri }); + } + + // Dijkstra + priority_queue, vector>, greater>> q; + vector dist(N+1, -1); + vector visited(N+1, false); + + q.push({ S, 1 }); + while (!q.empty()) { + long long currTime = q.top().first; + long long currNode = q.top().second; + q.pop(); + + visited[currNode] = true; + for (Path& p : crossroads[currNode]) { + unsigned long long waitingTime = (currTime % (p.g + p.r) < p.g) ? 0 : p.g + p.r - (currTime % (p.g + p.r)); + if (!visited[p.b] && (dist[p.b] < 0 || currTime + p.s + waitingTime < dist[p.b])) { + dist[p.b] = currTime + p.s + waitingTime; + q.push({ dist[p.b], p.b }); + } + } + } + + + for (long long i = 2; i <= N; i++) { + cout << dist[i] << " "; + } +} -- cgit v1.3.1-23-gd82d