Submission #1182942

#TimeUsernameProblemLanguageResultExecution timeMemory
1182942Zbyszek99Olympic Bus (JOI20_ho_t4)C++20
37 / 100
1095 ms5040 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define ff first #define ss second #define vl vector<long long> #define vi vector<int> #define pii pair<int,int> #define pll pair<long long, long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace std; //mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll rand(ll a, ll b) {return a + (mt() % (b-a+1));} const int INF = 1e9+50; const ll INF_L = 1e18+40; const ll MOD = 1e9+7; pii edge[50001]; ll cost[50001]; ll swap_cost[50001]; vector<pii> dij_graph[201]; ll ans = 1e18; ll dist[50001][6]; pii nxt[50001]; int n,m; void dij(int start, int type) { vi mam; mam.pb(start); vi nie_mam; rep2(i,1,n) if(i != start) nie_mam.pb(i); rep2(i,1,n) dist[i][type] = 1e18; rep2(i,1,n) nxt[i] = {0,0}; dist[start][type] = 0; rep(i,n-1) { pair<pll,pii> best = {{1e18,1},{1,1}}; forall(it,mam) { forall(e,dij_graph[it]) { if(dist[e.ff][type] != 1e18) continue; best = min(best,{{dist[it][type] + cost[e.ss],it},{e.ff,e.ss}}); } } if(best.ff.ff == 1e18) break; dist[best.ss.ff][type] = best.ff.ff; nxt[best.ss.ff] = {best.ff.ss,best.ss.ss}; mam.pb(best.ss.ff); rep(i,siz(nie_mam)) { if(nie_mam[i] == best.ss.ff) { swap(nie_mam[i],nie_mam[siz(nie_mam)-1]); nie_mam.pop_back(); break; } } } } void dij_fast(int start, int type) { rep2(i,1,n) dist[i][type] = 1e18; rep2(i,1,n) nxt[i] = {0,0}; priority_queue<pair<pair<ll,int>,pii>> pq; pq.push({{0,0},{start,0}}); while(!pq.empty()) { pair<pair<ll,int>,pii> t = pq.top(); pq.pop(); if(dist[t.ss.ff][type] != 1e18) continue; dist[t.ss.ff][type] = -t.ff.ff; nxt[t.ss.ff] = {t.ss.ss,t.ff.ss}; forall(it,dij_graph[t.ss.ff]) { pq.push({{t.ff.ff - cost[it.ss],it.ss},{it.ff,t.ss.ff}}); } } } void solve(int s, int t) { rep2(i,1,n) dij_graph[i] = {}; rep(i,m) { dij_graph[edge[i].ss].pb({edge[i].ff,i}); } dij_fast(t,1); dij_fast(s,5); rep2(i,1,n) dij_graph[i] = {}; rep(i,m) { dij_graph[edge[i].ff].pb({edge[i].ss,i}); } dij_fast(s,0); set<int> path_set; int cur = t; while(cur != s && cur != 0) { path_set.insert(nxt[cur].ss); cur = nxt[cur].ff; } dij_fast(t,4); ans = min(ans,dist[s][4] + dist[t][0]); cur = s; while(cur != t && cur != 0) { path_set.insert(nxt[cur].ss); cur = nxt[cur].ff; } rep(i,m) { if(path_set.find(i) == path_set.end()) { ans = min(ans,swap_cost[i] + min(dist[t][0],dist[edge[i].ss][0] + dist[edge[i].ff][1] + cost[i]) + min(dist[s][4],dist[edge[i].ss][4] + cost[i] + dist[edge[i].ff][5])); } else { swap(edge[i].ff,edge[i].ss); rep2(j,1,n) dij_graph[j] = {}; rep(j,m) { dij_graph[edge[j].ff].pb({edge[j].ss,j}); } dij(s,2); rep2(j,1,n) dij_graph[j] = {}; rep(j,m) { dij_graph[edge[j].ff].pb({edge[j].ss,j}); } dij(t,3); swap(edge[i].ff,edge[i].ss); ans = min(ans,dist[t][2] + dist[s][3] + swap_cost[i]); } } } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //random(); cin >> n >> m; rep(i,m) { int a,b; cin >> a >> b >> cost[i] >> swap_cost[i]; edge[i] = {a,b}; } solve(1,n); if(ans != 1e18) { cout << ans << "\n"; } else cout << "-1\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...