This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int int64_t
using namespace std;
const int LIM = 1e5, INF = 1e18;
int n, m, a[4][2*LIM], d[5*LIM];
map<int, vector<array<int, 2>>> g[LIM];
map<int, int> s[LIM];
set<array<int, 2>> q;
void add(int i, int j){
if(d[i] > j){
if(d[i] < INF) q.erase({d[i], i});
q.insert({-(d[i] = j), i});
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i=0; i<m; ++i){
for(int j=0; j<4; ++j) cin >> a[j][i];
--a[0][i], --a[1][i];
g[a[0][i]][a[2][i]].push_back({a[1][i], i});
g[a[1][i]][a[2][i]].push_back({a[0][i], i});
s[a[0][i]][a[2][i]] += a[3][i];
s[a[1][i]][a[2][i]] += a[3][i];
}
fill(d, d+n+m*2, INF);
q.insert({d[0] = 0, 0});
int ans = INF;
while(!q.empty()){
int dist = -(*q.begin())[0], u = (*q.begin())[1]; q.erase(q.begin());
if(dist != d[u]) continue;
if(u>=n){
int f = (u -= n) % m;
u = a[u >= m][f];
for(auto &[v, e] : g[u][a[2][f]])
if(e != f)
add(v, dist + s[u][a[2][e]] - a[3][e] - a[3][f]);
}
for(auto &[z, h] : g[u]){
for(auto &[v, e] : h){
add(v, dist + s[u][a[2][e]] - a[3][e]);
add(n+e+m*(u == a[0][e]), dist + a[3][e]);
}
}
if(u == n-1) ans = min(ans, dist);
}
cout << (ans == INF ? -1 : ans);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |