Submission #1087347

#TimeUsernameProblemLanguageResultExecution timeMemory
1087347quangminh412Commuter Pass (JOI18_commuter_pass)C++14
31 / 100
155 ms22488 KiB
#include <bits/stdc++.h> using namespace std; /* John Watson https://codeforces.com/profile/quangminh98 Mua Code nhu mua Florentino !! */ #define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long const ll oo = 1e18; const int maxn = 1e5 + 9; struct Near { int tar, id; ll w; Near(int tar, int id, ll w) : tar(tar), id(id), w(w) {} }; vector<Near> adj[maxn]; int state[2 * maxn]; int n, m, s, t, u, v; void dijkstra2(int s) { priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; vector<ll> dist(n + 2, oo); pq.push({dist[s] = 0, s}); while (!pq.empty()) { int u = pq.top().second; ll w = pq.top().first; pq.pop(); if (dist[u] != w) continue; for (Near nxt : adj[u]) { int v = nxt.tar; int id = nxt.id; ll ww = nxt.w; if (dist[v] > dist[u] + (state[id] == 1 ? 0 : ww)) pq.push({dist[v] = dist[u] + (state[id] == 1 ? 0 : ww), v}); } } cout << dist[v] << '\n'; } void dijkstra1(int s) { priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; vector<pair<int, int>> prev(n + 2, {0, 0}); vector<ll> dist(n + 2, oo); pq.push({dist[s] = 0, s}); while (!pq.empty()) { int u = pq.top().second; ll w = pq.top().first; pq.pop(); if (dist[u] != w) continue; for (Near nxt : adj[u]) { int v = nxt.tar; ll ww = nxt.w; int id = nxt.id; if (dist[v] > dist[u] + ww) { prev[v] = {u, id}; pq.push({dist[v] = dist[u] + ww, v}); } } } while (t != s) { state[prev[t].second] = 1; t = prev[t].first; } } vector<ll> shortestPath(int s) { priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; vector<ll> dist(n + 5, oo); pq.push({dist[s] = 0, s}); while (!pq.empty()) { int u = pq.top().second; ll w = pq.top().first; pq.pop(); if (dist[u] != w) continue; for (Near nxt : adj[u]) { int v = nxt.tar; ll ww = nxt.w; if (dist[v] > dist[u] + ww) pq.push({dist[v] = dist[u] + ww, v}); } } return dist; } void solve_1() { vector<ll> froms = shortestPath(s), fromt = shortestPath(t), fromv = shortestPath(v); ll ans = froms[v]; for (int vertex = 1; vertex <= n; vertex++) if (froms[vertex] + fromt[vertex] == froms[t]) ans = min(ans, fromv[vertex]); cout << ans << '\n'; } signed main() { if (fopen("test.inp", "r")) { freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout); } faster(); cin >> n >> m >> s >> t >> u >> v; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; ll w; cin >> w; adj[u].emplace_back(v, i, w); adj[v].emplace_back(u, i, w); } if (s == u) { solve_1(); return 0; } dijkstra1(s); dijkstra2(u); return 0; }

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:135:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |   freopen("test.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:136:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |   freopen("test.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...