Submission #736881

#TimeUsernameProblemLanguageResultExecution timeMemory
736881stevancvOlympic Bus (JOI20_ho_t4)C++14
0 / 100
31 ms7536 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 200 + 2; const int M = 5e4 + 2; const ll linf = 1e18; ll dist[N][N], di[N]; pair<int, int> prv[N]; vector<pair<int, int>> g[N]; int u[M], v[M], c[M], d[M], is[M], n, m; void Dijkstra(int x) { for (int i = 0; i < n; i++) di[i] = linf; vector<int> was(n); priority_queue<pair<ll, int>> pq; di[x] = 0; pq.push({0, x}); while (!pq.empty()) { ll v = -pq.top().first; int s = pq.top().second; pq.pop(); if (was[s] == 1) continue; was[s] = 1; for (auto zz : g[s]) { int u = zz.first; int i = zz.second; if (di[u] > v + c[i]) { di[u] = v + c[i]; prv[u] = {s, i}; pq.push({-di[u], u}); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dist[i][j] = linf; for (int i = 0; i < m; i++) { cin >> u[i] >> v[i] >> c[i] >> d[i]; u[i] -= 1; v[i] -= 1; dist[u[i]][v[i]] = c[i]; g[u[i]].push_back({v[i], i}); } for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) smin(dist[i][j], dist[i][k] + dist[k][j]); Dijkstra(0); if (di[n - 1] != linf) { int x = n - 1; while (x > 0) { is[prv[x].second] = 1; x = prv[x].first; } } Dijkstra(n - 1); if (di[0] != linf) { int x = 0; while (x < n - 1) { is[prv[x].second] = 1; x = prv[x].first; } } ll ans = linf; if (dist[0][n - 1] != linf && dist[n - 1][0] != linf) ans = dist[0][n - 1] + dist[n - 1][0]; for (int i = 0; i < m; i++) { if (is[i] == 0) { ll d1 = min(dist[0][n - 1], dist[0][v[i]] + c[i] + dist[u[i]][n - 1]); ll d2 = min(dist[n - 1][0], dist[n - 1][v[i]] + c[i] + dist[u[i]][0]); if (d1 < linf && d2 < linf) smin(ans, d1 + d2 + d[i]); continue; } for (int j = 0; j < n; j++) g[i].clear(); for (int j = 0; j < m; j++) { if (i == j) g[v[j]].push_back({u[j], j}); else g[u[j]].push_back({v[j], j}); } ll ret = d[i]; Dijkstra(0); if (di[n - 1] == linf) continue; ret += di[n - 1]; Dijkstra(n - 1); if (di[0] == linf) continue; ret += di[0]; smin(ans, ret); } if (ans == linf) ans = -1; cout << ans << en; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...