이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
const int INF = 1e16;
int n, m, ans = INF;
priority_queue<array<int, 2>> q;
vector<array<int, 3>> g[2][200];
void dijkstra(int r, int s, vector<int> &d, int i){
d.assign(n, INF);
d[s] = 0;
if(r == s) return;
q.push({0, s});
while(!q.empty()){
int u = q.top()[1], dist = -q.top()[0]; q.pop();
if(dist != d[u]) continue;
for(auto &[v, w, t] : g[i][u])
if(v != r && d[v] > dist + w)
q.push({-(d[v] = dist + w), v});
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
while(m--){
int u, v, w, d; cin >> u >> v >> w >> d;
--u, --v;
g[0][u].push_back({v, w, d});
g[1][v].push_back({u, w, d});
}
for(int u=0; u<n; ++u){
vector<int> d[2], e[2];
for(int i=0; i<2; ++i){
dijkstra(u, i ? n-1 : 0, d[i], 0);
dijkstra(u, i ? n-1 : 0, e[i], 1);
}
for(auto &[v, w, x] : g[1][u])
for(int j=0; j<2; ++j)
d[j][u] = min(d[j][u], w + d[j][v]);
array<int, 2> x[2], y[2];
for(int j=0; j<2; ++j) x[j] = y[j] = {INF, -1};
for(int i=0; i<(int)g[0][u].size(); ++i){
for(int j=0; j<2; ++j){
y[j] = min(y[j], {e[j][g[0][u][i][0]] + g[0][u][i][1], i});
if(x[j] > y[j]) swap(x[j], y[j]);
}
}
if(u == 0) x[0][0] = y[0][0] = 0;
if(u==n-1) x[1][0] = y[1][0] = 0;
for(int i=0; i<(int)g[0][u].size(); ++i){
auto &[v, w, t] = g[0][u][i];
array<int, 2> uE;
for(int j=0; j<2; ++j) uE[j] = x[j][1] == i ? y[j][0] : x[j][0];
int a = d[0][v] + w + uE[1];
int b = d[1][v] + w + uE[0];
ans = min(ans, a + t + min(e[0][n-1], d[1][u] + uE[0]));
ans = min(ans, b + t + min(e[1][0], uE[1] + d[0][u]));
ans = min(ans, a + b + t);
}
}
vector<int> d[2];
dijkstra(-1, 0, d[0], 0);
dijkstra(-1, n-1, d[1], 0);
ans = min(ans, d[0][n-1] + d[1][0]);
cout << (ans == INF ? -1 : ans) nl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |