Submission #897920

#TimeUsernameProblemLanguageResultExecution timeMemory
897920avighnaOlympic Bus (JOI20_ho_t4)C++17
37 / 100
1045 ms4992 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const ll N = 201, M = 50001; ll n, m; vector<pair<ll, ll>> adj[N]; ll c[M], d[M]; pair<ll, ll> edges[M]; vector<ll> sssp(ll source, ll i, bool b) { vector<ll> dist(n + 1, 1e15); dist[source] = 0; priority_queue<pair<ll, ll>> pq; pq.push({0, source}); while (!pq.empty()) { ll node = pq.top().second, _dist = -pq.top().first; pq.pop(); for (auto &nb : adj[node]) { if (edges[nb.second].first != node) { if (!(nb.second == i && b)) { continue; } } else if (nb.second == i and b) { continue; } if (dist[nb.first] > _dist + c[nb.second]) { dist[nb.first] = _dist + c[nb.second]; pq.push({-dist[nb.first], nb.first}); } } } return dist; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (ll i = 1; i <= n; ++i) { adj[i].clear(); } vector<vector<ll>> dist(n + 1, vector<ll>(n + 1, 1e15)); for (ll i = 1; i <= n; ++i) { dist[i][i] = 0; } for (ll i = 0, u, v; i < m; ++i) { cin >> u >> v >> c[i] >> d[i]; adj[u].push_back({v, i}); adj[v].push_back({u, i}); edges[i] = {u, v}; dist[u][v] = min(dist[u][v], c[i]); } for (ll k = 1; k <= n; ++k) { for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= n; ++j) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } ll ans = min((ll)1e15, dist[1][n] + dist[n][1]); for (ll i = 0; i < m; ++i) { ll u = edges[i].first, v = edges[i].second; if (min(dist[1][n], dist[1][v] + c[i] + dist[u][n]) + min(dist[n][1], dist[n][v] + c[i] + dist[u][1]) + d[i] < ans) { ans = min(ans, d[i] + sssp(1, i, 1)[n] + sssp(n, i, 1)[1]); } } if (ans == (ll)(1e15)) { 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...