#include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair iint; typedef pair llll; #define ALL(x) (x).begin(),(x).end() const ll zero = 0; const ll one = 1; const ll INF = 9223372036854775807; //10^18 const int inINF = 2147483647; //10^9 const ll MOD = 1000000007; //10^9+7 const ll MOD2 = 998244353; void Yes() {printf("Yes\n");} void No() {printf("No\n");} void YES() {printf("YES\n");} void NO() {printf("NO\n");} //dijkstraをする。Gはの配列。dは結果。vは始点。 void dijkstra(vector> &G, vector &d, vector &pre, ll v){ fill(ALL(d), INF / 2); d[v] = 0; priority_queue, greater> q; q.push(make_pair(0, v)); while(!q.empty()){ auto p = q.top(); q.pop(); auto pv = p.second; if(d[pv] < p.first) continue; //p.secondへの距離が更新済みならスルー for (auto next : G[pv]) { if(d[pv] + next.second >= d[next.first]) continue; d[next.first] = d[pv] + next.second; pre[next.first] = pv; q.push(make_pair(d[next.first], next.first)); } } } int main(){ ll N, M; cin >> N >> M; vector> G(N); map m; vector u(M), v(M), c(M), d(M); for (ll i = 0; i < M; i++) { cin >> u[i] >> v[i] >> c[i] >> d[i]; u[i]--; v[i]--; G[u[i]].push_back({v[i], c[i]}); G[v[i]].push_back({u[i], c[i]}); m[{u[i], v[i]}] = i; } vector dis(N), pre(N); dijkstra(G, dis, pre, 0); ll tmp = N-1; ll ans = dis[N-1]; for (ll i = 0; i < N; i++) { G[i].clear(); } vector ind; while(tmp != 0){ ll ttmp = pre[tmp]; bool ch = false; if(ttmp < tmp){ swap(ttmp, tmp); ch = true; } ind.push_back(m[{tmp, ttmp}]); if(ch == false){ swap(ttmp, tmp); } } int indd = 0; sort(ALL(ind)); for (int i = 0; i < M; i++) { if(ind[indd] == i){ G[u[i]].push_back({v[i], d[i]}); G[v[i]].push_back({u[i], d[i]}); indd++; } else{ G[u[i]].push_back({v[i], c[i]}); G[v[i]].push_back({u[i], c[i]}); } } // for (auto vv : m){ // printf("%lld %lld %lld\n", vv.first.first, vv.first.second, vv.second); // } // for (int i = 0; i < N; i++) { // cout << i << " " << pre[i] << endl; // } // for (int i = 0; i < ind.size(); i++) { // cout << ind[i] << endl; // } dijkstra(G, dis, pre, N-1); ans += dis[0]; printf("%lld\n", ans); }