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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 1e5 + 5, M = 2e5 + 5;
struct Edge{
int u, v, c, p;
Edge(){
}
Edge(int u, int v, int c, int p): u(u), v(v), c(c), p(p){
}
};
int n, m;
Edge edges[2 * M];
vi adj[N];
unordered_map <int, vi> mppc[N];
unordered_map <int, ll> mppsp[N];
unordered_map <int, ll> distc[N];
ll dist[N];
priority_queue <tuple <ll, int, int>, vector <tuple <ll, int, int>>, greater <tuple <ll, int, int>>> pq;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n >> m;
ForE(i, 1, m){
int u, v, c, p; cin >> u >> v >> c >> p;
edges[2 * i - 1] = Edge(u, v, c, p);
edges[2 * i] = Edge(v, u, c, p);
adj[u].emplace_back(2 * i - 1);
adj[v].emplace_back(2 * i);
}
memset(dist, 0x3f, sizeof(dist));
ForE(i, 1, 2 * m){
mppc[edges[i].u][edges[i].c].emplace_back(i);
mppsp[edges[i].u][edges[i].c] += edges[i].p;
distc[edges[i].u][edges[i].c] = dist[0];
}
dist[1] = 0; pq.emplace(dist[1], 1, 0);
while (!pq.empty()){
ll d; int u, c; tie(d, u, c) = pq.top(); pq.pop();
if (!c){
if (d != dist[u]){
continue;
}
Fora(elem, mppc[u]){
if (isz(elem.se) == 1){
int i = elem.se[0];
if (dist[edges[i].v] > d){
dist[edges[i].v] = d;
pq.emplace(dist[edges[i].v], edges[i].v, 0);
}
}
Fora(i, elem.se){
if (dist[edges[i].v] > d + edges[i].p){
dist[edges[i].v] = d + edges[i].p;
pq.emplace(dist[edges[i].v], edges[i].v, 0);
}
if (distc[edges[i].v][edges[i].c] > d){
distc[edges[i].v][edges[i].c] = d;
pq.emplace(d, edges[i].v, edges[i].c);
}
}
if (distc[u][elem.fi] > d){
distc[u][elem.fi] = d;
pq.emplace(d, u, elem.fi);
}
}
}
else{
if (d != distc[u][c]){
continue;
}
if (isz(mppc[u][c]) == 1){
continue;
}
Fora(i, mppc[u][c]){
if (dist[edges[i].v] > distc[u][c] + mppsp[u][c] - edges[i].p){
dist[edges[i].v] = distc[u][c] + mppsp[u][c] - edges[i].p;
pq.emplace(dist[edges[i].v], edges[i].v, 0);
}
}
}
}
cout << (dist[n] == dist[0] ? -1 : dist[n]) << endl;
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |