제출 #1293121

#제출 시각아이디문제언어결과실행 시간메모리
1293121TymondOlympic Bus (JOI20_ho_t4)C++20
0 / 100
1096 ms1844 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define fi first #define se second #define vi vector<int> #define vll vector<long long> #define pii pair<int, int> #define pll pair<long long, long long> #define pb push_back #define mp make_pair #define eb emplace_back #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rng64(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);} inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);} #ifdef DEBUG auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";} auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";} #define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X) #else #define debug(...){} #endif struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct pair_hash{ size_t operator()(const pair<int,int>&x)const{ return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32)); } }; struct Edge{ int v, u, c, d; Edge(){ v = u = c = d = -1; } Edge(int v1, int u1, int c1, int d1){ v = v1; u = u1; c = c1; d = d1; } }; const int MAXN = 207; const int MAXM = 5e4 + 7; const ll INF = 1e18; vi g[MAXN]; vi gt[MAXN]; ll dist[3][2][MAXN]; int back[3][2][MAXN]; Edge edges[MAXM]; int used[MAXN]; int n, m; int special[MAXM]; void Dijkstra(vi graph[], int cur, int st, int rev = -1){ int start = 1; if(st){ start = n; } for(int i = 1; i <= n; i++){ dist[cur][st][i] = INF; used[i] = 0; } dist[cur][st][start] = 0LL; for(int k = 1; k <= n; k++){ int best = -1; for(int i = 1; i <= n; i++){ if(used[i]){ continue; } if(best == -1 || dist[cur][st][i] < dist[cur][st][best]){ best = i; } } if(best == -1){ break; } used[best] = 1; if(rev != -1 && best == edges[rev].u && dist[cur][st][edges[rev].v] > dist[cur][st][best] + edges[rev].c){ dist[cur][st][edges[rev].v] = dist[cur][st][best] + edges[rev].c; } for(auto e : graph[best]){ if(rev == e){ continue; } dist[cur][st][edges[e].u] = min(dist[cur][st][edges[e].u], dist[cur][st][best] + edges[e].c); if(dist[cur][st][edges[e].u] == dist[cur][st][best] + edges[e].c){ back[cur][st][edges[e].u] = e; } } } } void findSpecial(){ int curr = n; if(dist[0][0][n] != INF){ while(curr != 1){ special[back[0][0][curr]] = 1; curr = edges[back[0][0][curr]].v; } } curr = 1; if(dist[0][1][1] != INF){ while(curr != n){ special[back[0][1][curr]] = 1; curr = edges[back[0][1][curr]].v; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for(int i = 1; i <= m; i++){ int a, b, c, d; cin >> a >> b >> c >> d; g[a].pb(i); gt[b].pb(i); edges[i] = Edge(a, b, c, d); } Dijkstra(g, 0, 0); Dijkstra(g, 0, 1); Dijkstra(gt, 1, 0); Dijkstra(gt, 1, 1); findSpecial(); ll ans = dist[0][0][n] + dist[0][1][1]; for(int i = 1; i <= m; i++){ // cerr << "------\n"; if(special[i]){ // cerr << "SPECIAL: " << i << '\n'; Dijkstra(g, 2, 0, i); Dijkstra(g, 2, 1, i); ans = min(ans, (ll)dist[2][0][n] + dist[2][1][1] + edges[i].d); }else{ ll now = min(dist[0][0][n], dist[0][0][edges[i].u] + edges[i].c + dist[1][1][edges[i].v]) + edges[i].d; now += min(dist[0][1][1], dist[0][1][edges[i].u] + edges[i].c + dist[1][0][edges[i].v]); // cerr << min(dist[0][1][1], dist[0][1][edges[i].u] + edges[i].c + dist[1][0][edges[i].v]) << '\n'; // cerr << now << '\n'; ans = min(ans, now); } } if(ans >= INF){ cout << "-1\n"; }else{ cout << ans << '\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...