// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 2e18 + 7;
/*
there are two case to deal with:
- fix the edge of a to elm
- fix all other edge that have the same col as a-elm edge:
+ move to a with random edge
+ move to a with the same col edge
*/
const int maxn = 2e5 + 10;
int n, m;
vector<array<int, 3>> adj[maxn];
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;
vector<ll> dist(maxn, INF);
ll sum[maxn];//sum[i] = sum all p of edge that have col i adjacent to a
ll minn[maxn];//minn[i] = min d[elm] that have c(elm, a) = i
void solve(){
cin >> n >> m;
for(int i=1; i<=m; ++i){
int a, b, c, p;cin >> a >> b >> c >> p;
adj[a].push_back({b, c, p});
adj[b].push_back({a, c, p});
}
pq.push({0, 1});
dist[1] = 0;
while(!pq.empty()){
int a = pq.top().se;
int d = pq.top().fi;
pq.pop();
if(d > dist[a])continue;
for(auto &[elm, col, p]: adj[a]){
sum[col] = 0;
minn[col] = INF;
}
for(auto &[elm, col, p]: adj[a]){
sum[col] += p;
minn[col] = min(minn[col], dist[elm]);
}
for(auto &[elm, col, p]: adj[a]){
ll nxt = min({
dist[a] + p,
dist[a] + sum[col] - p,
minn[col] + sum[col] - p
});
if(dist[elm] > nxt){
dist[elm] = nxt;
pq.push({dist[elm], elm});
}
}
}
if(dist[n] == INF)cout << -1 << '\n';
else cout << dist[n] << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |