제출 #705684

#제출 시각아이디문제언어결과실행 시간메모리
705684cig32Olympic Bus (JOI20_ho_t4)C++17
37 / 100
1077 ms7048 KiB
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define int long long using namespace std; struct edge { int u, v, c, d; }; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; edge e[m+1]; for(int i=1; i<=m; i++) { cin >> e[i].u >> e[i].v >> e[i].c >> e[i].d; } int dist1[n+1][n+1], distn[n+1][n+1]; int rev1[n+1][n+1], revn[n+1][n+1]; // b=0: nothing for(int i=0; i<=n; i++) { for(int j=0; j<=n; j++) { dist1[i][j] = distn[i][j] = rev1[i][j] = revn[i][j] = 1e18; } dist1[i][1] = 0; distn[i][n] = 0; rev1[i][1] = 0; revn[i][n] = 0; } for(int b=0; b<=n; b++) { vector<pair<int, int> > adj[n+1], rev[n+1]; bool vis[n+1]; for(int i=1; i<=m; i++) { if(e[i].u != b && e[i].v != b) { adj[e[i].u].push_back({e[i].v, e[i].c}); rev[e[i].v].push_back({e[i].u, e[i].c}); } } priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq; for(int i=1; i<=n; i++) pq.push({dist1[b][i], i}); for(int i=1; i<=n; i++) vis[i] = 0; while(pq.size()) { pair<int,int> t = pq.top(); pq.pop(); if(!vis[t.second]) { for(pair<int, int> x: adj[t.second]) { if(!vis[x.first] && dist1[b][x.first] > dist1[b][t.second]+x.second) { dist1[b][x.first] = dist1[b][t.second]+x.second; pq.push({dist1[b][x.first], x.first}); } } } } for(int i=1; i<=n; i++) pq.push({distn[b][i], i}); for(int i=1; i<=n; i++) vis[i] = 0; while(pq.size()) { pair<int,int> t = pq.top(); pq.pop(); if(!vis[t.second]) { for(pair<int, int> x: adj[t.second]) { if(!vis[x.first] && distn[b][x.first] > distn[b][t.second]+x.second) { distn[b][x.first] = distn[b][t.second]+x.second; pq.push({distn[b][x.first], x.first}); } } } } for(int i=1; i<=n; i++) pq.push({rev1[b][i], i}); for(int i=1; i<=n; i++) vis[i] = 0; while(pq.size()) { pair<int,int> t = pq.top(); pq.pop(); if(!vis[t.second]) { for(pair<int, int> x: rev[t.second]) { if(!vis[x.first] && rev1[b][x.first] > rev1[b][t.second]+x.second) { rev1[b][x.first] = rev1[b][t.second]+x.second; pq.push({rev1[b][x.first], x.first}); } } } } for(int i=1; i<=n; i++) pq.push({revn[b][i], i}); for(int i=1; i<=n; i++) vis[i] = 0; while(pq.size()) { pair<int,int> t = pq.top(); pq.pop(); if(!vis[t.second]) { for(pair<int, int> x: rev[t.second]) { if(!vis[x.first] && revn[b][x.first] > revn[b][t.second]+x.second) { revn[b][x.first] = revn[b][t.second]+x.second; pq.push({revn[b][x.first], x.first}); } } } } } int ans = dist1[0][n] + distn[0][1]; vector<pair<int, int> > adj[n+1], rev[n+1]; vector<int> adj_id[n+1]; for(int i=1; i<=m; i++) { adj_id[e[i].u].push_back(i); adj[e[i].u].push_back({e[i].v, e[i].c}); rev[e[i].v].push_back({e[i].u, e[i].c}); } int cnt[n+1][n+1]; for(int i=0; i<=n; i++) { for(int j=0; j<=n; j++) { cnt[i][j] = 0; } } for(int i=1; i<=m; i++) { cnt[e[i].u][e[i].v]++; } for(int i=1; i<=m; i++) { int forward, backward; forward = dist1[e[i].u][e[i].v] + e[i].c; int mi = (e[i].u == n ? 0 : 1e18); for(pair<int,int> x: adj[e[i].u]) { mi = min(mi, x.second + revn[e[i].u][x.first]); } forward += mi; forward = min(forward, dist1[e[i].u][n]); int pt1 = (e[i].u == 1 ? 0 : 1e18); int pt2 = (e[i].u == n ? 0 : 1e18); for(pair<int,int> x: rev[e[i].u]) { pt1 = min(pt1, dist1[e[i].u][x.first] + x.second); } int it=0; for(pair<int,int> x: adj[e[i].u]) { if(adj_id[e[i].u][it] == i) { it++; continue; } it++; pt2 = min(pt2, revn[e[i].u][x.first] + x.second); } forward = min(forward, pt1 + pt2); backward = distn[e[i].u][e[i].v] + e[i].c; mi = (e[i].u == 1 ? 0 : 1e18); for(pair<int,int> x: adj[e[i].u]) { mi = min(mi, x.second + rev1[e[i].u][x.first]); } backward += mi; backward = min(backward, distn[e[i].u][1]); pt1 = (e[i].u == n ? 0 : 1e18); pt2 = (e[i].u == 1 ? 0 : 1e18); for(pair<int,int> x: rev[e[i].u]) { pt1 = min(pt1, distn[e[i].u][x.first] + x.second); } it=0; for(pair<int,int> x: adj[e[i].u]) { if(adj_id[e[i].u][it] == i) { it++; continue; } it++; pt2 = min(pt2, rev1[e[i].u][x.first] + x.second); } backward = min(backward, pt1 + pt2); ans = min(ans, forward + backward + e[i].d); //cout << "flip edge " << i << ": " << forward << " + " << backward << " + " << e[i].d << " = " << forward + backward + e[i].d << "\n"; } cout << (ans <= 1e12 ? ans : -1) << "\n"; } /* 5 6 1 2 1 10000 2 5 1 10000 5 3 1 10000 2 3 10 1 2 4 1 10000 4 1 1 10000 2 2 1 2 3 4 1 2 3 9 3 4 1 2 3 4 2 3 3 9 2 1 3 10 2 3 3 11 2 7 1 2 1 1 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 5 1 2 1 6 1 2 1 9 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...