Submission #203840

#TimeUsernameProblemLanguageResultExecution timeMemory
203840staniewzkiOlympic Bus (JOI20_ho_t4)C++17
0 / 100
230 ms2104 KiB
#include<bits/stdc++.h> using namespace std; ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.first << ", " << p.second << ")"; } template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << "{"; for(auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << "}"; } void dump() { cerr << "\n"; } template<class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #ifdef DEBUG # define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else # define debug(...) false #endif #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second template<class T> int size(T && a) { return a.size(); } using LL = long long; using PII = pair<int, int>; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<int> a(m), b(m), c(m), d(m); vector<vector<int>> g(n), r(n); REP(i, m) { cin >> a[i] >> b[i] >> c[i] >> d[i]; a[i]--, b[i]--; g[a[i]].emplace_back(i); r[b[i]].emplace_back(i); } vector<bool> on_path(m); auto dijkstra = [&](vector<vector<int>> &adj, int source, int banned, bool mark) { vector<int> dst(n, 1e9), last(n, -1); priority_queue<PII> Q; dst[source] = 0; Q.emplace(0, source); while(!Q.empty()) { int d, v; tie(d, v) = Q.top(); Q.pop(); if(-d != dst[v]) continue; for(int e : adj[v]) { if(banned == e) continue; int u = (a[e] == v ? b[e] : a[e]); if(dst[u] > dst[v] + c[e]) { if(mark) last[u] = e; dst[u] = dst[v] + c[e]; Q.emplace(-dst[u], u); } } } REP(i, n) if(last[i] != -1) on_path[last[i]] = true; return dst; }; auto norm_source = dijkstra(g, 0, -1, true); auto norm_sink = dijkstra(g, n - 1, -1, true); auto rev_source = dijkstra(r, 0, -1, false); auto rev_sink = dijkstra(r, n - 1, -1, false); auto add_edge = [&](int i) { g[b[i]].emplace_back(size(a)); r[a[i]].emplace_back(size(a)); a.emplace_back(b[i]); b.emplace_back(a[i]); c.emplace_back(c[i]); }; auto pop_edge = [&](int i) { g[b[i]].pop_back(); r[a[i]].pop_back(); a.pop_back(); b.pop_back(); c.pop_back(); }; int ans = norm_source[n - 1] + norm_sink[0]; REP(i, m) { if(on_path[i]) { add_edge(i); auto source = dijkstra(g, 0, i, false); auto sink = dijkstra(g, n - 1, i, false); ans = min(ans, source[n - 1] + sink[0] + d[i]); pop_edge(i); } else { int source = min(norm_source[n - 1], norm_source[b[i]] + c[i] + rev_sink[a[i]]); int sink = min(norm_sink[0], norm_sink[b[i]] + c[i] + rev_source[a[i]]); ans = min(ans, source + sink + d[i]); } } if(ans >= 1e9) ans = -1; cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...