제출 #827571

#제출 시각아이디문제언어결과실행 시간메모리
827571taherOlympic Bus (JOI20_ho_t4)C++17
16 / 100
33 ms4168 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "C:\GCC\debug.h" #else #define debug(...) void(42) #endif const long long inf = 123165465792545; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<array<int, 4>>> adj(n); vector<vector<array<int, 4>>> inv(n); vector<array<int, 4>> edges; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; int c, d; cin >> c >> d; --u, --v; adj[u].push_back({v, c, d, i}); inv[v].push_back({u, c, d, i}); edges.push_back({u, v, c, d}); } array<int, 2> perm; perm[0] = 0; perm[1] = n - 1; vector<vector<long long>> d2(n, vector<long long> (n, inf)); auto Floyd = [&]() { for (int i = 0; i < n; i++) { d2[i][i] = 0; } for (int i = 0; i < n; i++) { for (auto v : adj[i]) { d2[i][v[0]] = min(d2[i][v[0]], 1LL * v[1]); } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d2[i][j] = min(d2[i][j], d2[i][k] + d2[k][j]); } } } }; Floyd(); auto Update = [&](int forbidden, int source, int to, int cost) { vector<long long> dist(n, inf); dist[perm[1]] = 0; priority_queue<array<long long, 2>, vector<array<long long, 2>>, greater<array<long long, 2>>> que; que.push({0, perm[1]}); while (!que.empty()) { auto cur = que.top(); que.pop(); if (dist[cur[1]] != cur[0]) { continue; } if (cur[1] == source) { if (dist[to] > 1LL * cur[0] + cost) { dist[to] = 1LL * cur[0] + cost; que.push({dist[to], to}); } } for (auto v : adj[cur[1]]) { if (v[3] == forbidden) { continue; } else { if (dist[v[0]] > cur[0] + v[1]) { dist[v[0]] = cur[0] + v[1]; que.push({dist[v[0]], v[0]}); } } } } return dist[perm[0]]; }; vector<int> cntA(n); vector<int> cntB(n); vector<int> middleA(n, -1); vector<int> middleB(n, -1); for (int i = 0; i < n; i++) { for (auto v : inv[i]) { if (d2[perm[0]][v[0]] + v[1] == d2[perm[0]][i]) { cntA[i]++; middleA[i] = v[0]; } } } for (int i = 0; i < n; i++) { for (auto v : adj[i]) { if (d2[v[0]][perm[1]] + v[1] == d2[i][perm[1]]) { cntB[i]++; middleB[i] = v[0]; } } } vector<int> cntC(n); vector<int> cntD(n); vector<int> middleC(n, -1); vector<int> middleD(n, -1); for (int i = 0; i < n; i++) { for (auto v : inv[i]) { if (d2[perm[1]][v[0]] + v[1] == d2[perm[1]][i]) { cntC[i]++; middleC[i] = v[0]; } } } for (int i = 0; i < n; i++) { for (auto v : adj[i]) { if (d2[v[0]][perm[0]] + v[1] == d2[i][perm[0]]) { cntD[i]++; middleD[i] = v[0]; } } } long long res = inf; res = min(res, d2[perm[0]][perm[1]] + d2[perm[1]][perm[0]]); for (int i = 0; i < m; i++) { int u = edges[i][0]; int v = edges[i][1]; int c = edges[i][2]; int d = edges[i][3]; long long path_from_beg = 0; bool canA = true; bool canB = true; canA &= (cntA[v] > 1 || middleA[v] != u); canB &= (cntB[u] > 1 || middleB[u] != v); if (canA && canB) { path_from_beg = min(d2[perm[0]][perm[1]], d2[perm[0]][v] + d2[u][perm[1]] + c); } else { swap(perm[0], perm[1]); path_from_beg = Update(i, v, u, c); swap(perm[0], perm[1]); } canA = true; canB = true; canA &= (cntC[v] > 1 || middleC[v] != u); canB &= (cntD[u] > 1 || middleD[u] != v); long long path_from_end = 0; if (canA && canB) { path_from_end = min(d2[perm[1]][perm[0]], d2[perm[1]][v] + d2[u][perm[0]] + c); } else { path_from_end = Update(i, v, u, c); } long long tot = path_from_beg + path_from_end + d; res = min(res, tot); } if (res == inf) { cout << -1 << '\n'; } else { cout << res << '\n'; } 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...