#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
#define arr5 array<ll, 5>
const ll inf = 1e18;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m; cin>>n>>m;
vector<arr3> g[n + 1];
map<int, ll> mp[n + 1];
while (m--){
int x, y, c, w; cin>>x>>y>>c>>w;
g[x].pb({y, c, w});
g[y].pb({x, c, w});
mp[x][c] += w; mp[y][c] += w;
}
queue<arr5> q; q.push({0, 1, 0, 0, 0});
vector<vector<ll>> dist(n + 1, vector<ll>(n + 1, inf));
dist[1][0] = 0;
while (!q.empty()){
auto [d, v, C, W, P] = q.front(); q.pop();
if (d != dist[v][P]) continue;
for (auto [u, c, w]: g[v]){
ll f = d + w;
if (f < dist[u][v]){
dist[u][v] = f;
q.push({f, u, c, w, v});
}
ll s = (mp[v][c] - (c == C) * W) - w; f = d + s;
if (f < dist[u][0]){
dist[u][0] = f;
q.push({f, u, 0, 0, 0});
}
}
}
ll out = inf;
for (int i = 0; i <= n; i++){
out = min(out, dist[n][i]);
}
cout<<((out == inf) ? -1 : out)<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |