#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define L(i, b, e) for (int i = b; i < e; ++i)
#define R(i, b, e) for (int i = b; i >= e; --i)
#define pb emplace_back
#define vi vector<int>
#define sz(x) ((int) x.size())
const int N = 2e5 + 7;
int n, m;
vector<pair<int, int>> g[N];
int c[N], p[N];
ll d[N];
const ll inf = 1e18;
ll mn[N];
ll sum[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
L(i, 0, m){
int a, b;
cin >> a >> b >> c[i] >> p[i];
a--;
b--;
g[a].pb(b, i);
g[b].pb(a, i);
}
L(i, 0, m + 1)mn[i] = inf;
L(i, 0, n)d[i] = inf;
using T = pair<ll, int>;
priority_queue<T, vector<T>, greater<T>> pq;
d[0] = 0;
pq.push({0, 0});
while (!pq.empty()){
auto [w, x] = pq.top();
pq.pop();
if (d[x] != w)continue;
for (auto &j : g[x]){
sum[c[j.second]] += p[j.second];
mn[c[j.second]] = min(mn[c[j.second]], d[j.first]);
}
for (auto &[i, j] : g[x]){
// change the color of road
ll t = min({w + p[j], w + sum[c[j]] - p[j], mn[c[j]] + sum[c[j]] - p[j]});
if (t < d[i]){
d[i] = t;
pq.push({d[i], i});
}
}
for (auto &j : g[x]){
sum[c[j.second]] = 0;
mn[c[j.second]] = inf;
}
}
if (d[n - 1] == inf){
cout << -1;
}else{
cout << d[n - 1];
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |