//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") #include //#include //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please vector> E[100001]; int C[100001], D[100001]; pair mae[100001]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; rep(i, M) { int u, v, c, d; cin >> u >> v >> c >> d; E[u].pb({ v, i }); E[v].pb({ u, i }); C[i] = c; D[i] = d; } #define PT pair ll Di[100001]; rep1(i, N) Di[i] = 1e18; priority_queue, greater> que; Di[1] = 0; que.push(mp(0, 1)); while (que.size()) { PT p = que.top(); que.pop(); int i = p.second; if (Di[i] != p.first) continue; for (PT p2 : E[i]) { int j = p2.first; int cc = C[p2.second]; int dd = C[p2.second]; ll d = Di[i] + cc; if (Di[j] > d) { Di[j] = d; mae[j] = { i, p2.second }; que.push(mp(d, j)); } } } int tmp = N; while (mae[tmp].first) { C[mae[tmp].second] = D[mae[tmp].second]; tmp = mae[tmp].first; } ll are = Di[N]; rep1(i, N) Di[i] = 1e18; Di[1] = 0; que.push(mp(0, 1)); while (que.size()) { PT p = que.top(); que.pop(); int i = p.second; if (Di[i] != p.first) continue; for (PT p2 : E[i]) { int j = p2.first; int cc = C[p2.second]; int dd = C[p2.second]; ll d = Di[i] + cc; if (Di[j] > d) { Di[j] = d; mae[j] = { i, p2.second }; que.push(mp(d, j)); } } } co(are + Di[N]); Would you please return 0; }