Submission #206919

#TimeUsernameProblemLanguageResultExecution timeMemory
206919savacskaOlympic Bus (JOI20_ho_t4)C++14
100 / 100
144 ms2936 KiB
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define x first #define y second using namespace std; typedef long long ll; const ll INF = (ll) 1e18; vector <pair <int, int> > g[205], rev[205]; ll start1[205], finish1[205], start2[205], finish2[205], len[50005], cost[50005]; ll dist[205]; pair <int, int> back[205], forw[205], rebr[50005]; bool used[205], sf[50005], fs[50005]; void dijkstra(int n, int start, ll dist[], pair <int, int> parent[], vector <pair <int, int> > g[]) { for (int i = 1; i <= n; i++) dist[i] = INF, used[i] = false, parent[i] = mp(0, 0); dist[start] = 0; for (int rep = 1; rep <= n; rep++) { int v = -1; for (int i = 1; i <= n; i++) if (!used[i] && (v == -1 || dist[v] > dist[i])) v = i; if (dist[v] == INF) break; used[v] = true; for (int i = 0; i < (int) g[v].size(); i++) { int u = g[v][i].x, num = g[v][i].y; if (dist[u] > dist[v] + len[num]) dist[u] = dist[v] + len[num], parent[u] = mp(v, num); } } return; } ll schet(int n, int start, int finish, int num) { for (int i = 1; i <= n; i++) dist[i] = INF, used[i] = false; dist[start] = 0; for (int rep = 1; rep <= n; rep++) { int v = -1; for (int i = 1; i <= n; i++) if (!used[i] && (v == -1 || dist[v] > dist[i])) v = i; if (dist[v] == INF) break; used[v] = true; for (int i = 0; i < (int) g[v].size(); i++) { int u = g[v][i].x, nm = g[v][i].y; if (nm == num) continue; dist[u] = min(dist[u], dist[v] + len[nm]); } if (v == rebr[num].y) dist[rebr[num].x] = min(dist[rebr[num].x], dist[v] + len[num]); } return dist[finish]; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v >> len[i] >> cost[i]; rebr[i] = mp(u, v); g[u].pb(mp(v, i)), rev[v].pb(mp(u, i)); } dijkstra(n, 1, start2, back, rev); dijkstra(n, n, finish2, forw, rev); dijkstra(n, 1, start1, back, g); dijkstra(n, n, finish1, forw, g); int tek = n; while (back[tek].y > 0) { sf[back[tek].y] = true; tek = back[tek].x; } tek = 1; while (forw[tek].y > 0) { fs[forw[tek].y] = true; tek = forw[tek].x; } ll ans = min(INF, start1[n] + finish1[1]); for (int i = 1; i <= m; i++) { ll cs1 = 0, cs2 = 0; if (sf[i]) cs1 = schet(n, 1, n, i); else cs1 = min(start1[n], start1[rebr[i].y] + len[i] + finish2[rebr[i].x]); if (fs[i]) cs2 = schet(n, n, 1, i); else cs2 = min(finish1[1], finish1[rebr[i].y] + len[i] + start2[rebr[i].x]); ans = min(ans, cs1 + cs2 + cost[i]); } if (ans != INF) cout << ans << '\n'; else cout << "-1\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...