Submission #364308

#TimeUsernameProblemLanguageResultExecution timeMemory
364308shafinalamOlympic Bus (JOI20_ho_t4)C++14
0 / 100
27 ms5024 KiB
#include <bits/stdc++.h> using namespace std; const int mxn = 1e5+5; typedef long long ll; #define inf 1e17 typedef pair<ll,ll>pii; ll cost[mxn]; ll dir[mxn]; struct data { int u, v; ll c, d; }; vector<pii>adj[205]; vector<pii>rev[205]; vector<data>edge; ll dis[5][205]; int path[3][205]; int n, m; void dijkstra1(int src, int pos) { vector<int>vis(n+1, 0); dis[pos][src] = 0; priority_queue<pii>pq; pq.push({0LL, src}); while(!pq.empty()) { int u = pq.top().second; pq.pop(); if(vis[u]) continue; vis[u] = 1; for(auto it : adj[u]) { ll w = cost[it.second]; int v = it.first; if(dis[pos][u]+w<dis[pos][v]) { dis[pos][v] = dis[pos][u]+w; path[pos][v] = it.second; pq.push({-dis[pos][v], v}); } } } return; } void dijkstra2(int src, int pos) { vector<int>vis(n+1, 0); dis[pos][src] = 0; priority_queue<pii>pq; pq.push({0LL, src}); while(!pq.empty()) { int u = pq.top().second; pq.pop(); if(vis[u]) continue; vis[u] = 1; for(auto it : rev[u]) { ll w = cost[it.second]; int v = it.first; if(dis[pos][u]+w<dis[pos][v]) { dis[pos][v] = dis[pos][u]+w; pq.push({-dis[pos][v], v}); } } } return; } ll dijkstra(int src, int idx) { vector<ll>dp(n+5, inf); vector<bool>vis(n+5, 0); dp[src] = 0; priority_queue<pii>pq; while(!pq.empty()) { int u = pq.top().second; pq.pop(); if(vis[u]) continue; vis[u] = 1; for(auto it : adj[u]) { if(idx==it.second) continue; int v = it.first; ll w = cost[it.second]; if(dp[u]+w<dp[v]) { dp[v] = dp[u]+w; pq.push({-dp[v], v}); } } if(u==edge[idx].v) { int x = edge[idx].v; int y = edge[idx].u; ll w = edge[idx].c; if(dp[x]+w<dp[y]) { dp[y] = dp[x]+w; pq.push({-dp[y], y}); } } } if(src==1) return dp[n]; return dp[1]; } int main() { scanf("%d%d", &n, &m); for(int i = 0; i < 5; i++) { for(int r = 1; r <= n; r++) dis[i][r] = inf; } for(int i = 0; i < m; i++) { int u, v; scanf("%d%d%lld%lld", &u, &v, &cost[i], &dir[i]); adj[u].push_back({v, i}); rev[v].push_back({u, i}); edge.push_back({u, v, cost[i], dir[i]}); } dijkstra1(1, 0); dijkstra1(n, 1); dijkstra2(1, 2); dijkstra2(n, 3); ll ans = dis[0][n]+dis[1][1]; for(int i = 0; i < m; i++) { int u = edge[i].u; int v = edge[i].v; ll c = edge[i].c; ll d = edge[i].d; ll res = d; if(path[0][v]==1) { res+=dijkstra(1, i); } else { res+=min(dis[0][n], dis[0][v]+dis[4][u]+c); } if(path[1][v]==1) { res+=dijkstra(n, i); } else { res+=min(dis[1][1], dis[1][v]+dis[3][u]+c); } ans = min(ans, res); } if(ans>=inf) { cout << -1 << '\n'; } else { cout << ans << '\n'; } return 0; }

Compilation message (stderr)

ho_t4.cpp: In function 'int main()':
ho_t4.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:113:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  113 |         scanf("%d%d%lld%lld", &u, &v, &cost[i], &dir[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...