This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Calm down.
// Think three times, code twice.
#include "bits/stdc++.h"
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define fore(x, v) for(auto &x : v)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <ll,int>
#define pll pair <ll,ll>
#define piii pair <ll,pii>
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const ll oo = 1e18;
int n, m;
map<int, vector<pii>> graph[N];
ll dp[N];
map<int, ll> dp2[N], sum[N];
void to_nho_cau() {
cin >> n >> m;
forr(i, 1, m) {
int u, v, c, p;
cin >> u >> v >> c >> p;
graph[u][c].emplace_back(v, p);
graph[v][c].emplace_back(u, p);
sum[u][c] += p;
sum[v][c] += p;
}
memset(dp, 0x3f, sizeof(dp));
priority_queue<pair<ll, pii>> pq;
dp[1] = 0;
pq.push({0, {1, 0}});
while (!pq.empty()) {
auto top = pq.top(); pq.pop();
ll cost = top.st;
int u = top.nd.st, c = top.nd.nd;
if (!c) {
if (-cost != dp[u]) continue;
fore(nc, graph[u]) fore(info, nc.nd) {
int v = info.st, p = info.nd;
ll ncost;
ncost = sum[u][nc.st] - p - cost;
if (ncost < dp[v]) {
dp[v] = ncost;
pq.push({-ncost, {v, 0}});
}
ncost = p - cost;
if (ncost < dp[v]) {
dp[v] = ncost;
pq.push({-ncost, {v, 0}});
}
ncost = -cost;
if ((dp2[v].find(nc.st) == dp2[v].end()) || ncost < dp2[v][nc.st]) {
dp2[v][nc.st] = ncost;
pq.push({-ncost, {v, nc.st}});
}
}
}
else {
if (-cost != dp2[u][c]) continue;
ll ncost = sum[u][c] - cost;
fore(info, graph[u][c]) {
int v = info.st, p = info.nd;
if (ncost - p < dp[v]) {
dp[v] = ncost - p;
pq.push({-ncost + p, {v, 0}});
}
}
}
}
cout << (dp[n] < oo ? dp[n] : -1);
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
freopen(file".inp","r",stdin);
freopen(file".out","w",stdout);
#endif // LOCAL
int t = 1;
// cin >> t;
while(t--) to_nho_cau();
}
/*
1.self check:
2.long long
3.size of array
4.code for testing
5.initializing
6.modulo number
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |