Submission #897934

#TimeUsernameProblemLanguageResultExecution timeMemory
897934avighnaOlympic Bus (JOI20_ho_t4)C++17
37 / 100
1069 ms7124 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const ll N = 201, M = 50001; ll n, m; struct Edge { ll u, v, i; }; multiset<pair<ll, ll>> adj[N]; ll c[M], d[M]; Edge edges[M]; vector<ll> sssp(ll source) { 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 (dist[nb.first] > _dist + c[nb.second]) { dist[nb.first] = _dist + c[nb.second]; pq.push({-dist[nb.first], nb.first}); } } } return dist; } vector<ll> adjall[N]; bool vis[N]; void dfs(ll node) { if (vis[node]) { return; } vis[node] = true; for (auto &i : adjall[node]) { dfs(i); } } 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].insert({v, i}), adjall[u].push_back(v), adjall[v].push_back(u); edges[i].u = u, edges[i].v = v, edges[i].i = i; dist[u][v] = min(dist[u][v], c[i]); } dfs(1); if (!vis[n]) { cout << "-1\n"; return 0; } 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]); sort(edges, edges + m, [&dist](const Edge &e1, const Edge &e2) { ll u1 = e1.u, u2 = e2.u, v1 = e1.v, v2 = e2.v, i1 = e1.i, i2 = e2.i; ll d1 = min(dist[1][n], dist[1][v1] + c[i1] + dist[u1][n]) + min(dist[n][1], dist[n][v1] + c[i1] + dist[u1][1]) + d[i1]; ll d2 = min(dist[1][n], dist[1][v2] + c[i2] + dist[u2][n]) + min(dist[n][1], dist[n][v2] + c[i2] + dist[u2][1]) + d[i2]; return d1 < d2; }); for (ll i = 0; i < m; ++i) { ll u = edges[i].u, v = edges[i].v, _i = edges[i].i; 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) { adj[u].erase(adj[u].find({v, _i})); adj[v].insert({u, _i}); ans = min(ans, d[_i] + sssp(1)[n] + sssp(n)[1]); adj[v].erase(adj[v].find({u, _i})); adj[u].insert({v, _i}); } } 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...