#include //#include using namespace std; //using namespace atcoder; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef long long ll; typedef pair P; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template T gcd(T a,T b){return b?gcd(b,a%b):a;} const ll mod=998244353; const ll LINF=1ll<<60; const int INF=1<<30; int dx[]={1,0,-1,0,1,-1,1,-1}; int dy[]={0,1,0,-1,1,-1,-1,1}; struct edge {int to;ll cost;}; const int M_N=300000; vector G[M_N+1]; struct Dijkstra{ ll d[M_N+1]; ll b[M_N+1]; void init(int n){ fill(d,d+n,LINF); fill(b,b+n,0); } void dijkstra(int s){ priority_queue,greater

> que; que.push(P(0, s)); d[s] = 0; while(!que.empty()){ P p=que.top(); que.pop(); int v=p.second; if(d[v]d[v]+e.cost){ d[e.to]=d[v]+e.cost; que.push(P(d[e.to],e.to)); b[e.to]=v; } } } } }; int main(){ int n,m;cin >> n >> m; vector a(m); auto b = a; auto c = a; auto d = a; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i] >> c[i] >> d[i]; a[i]--,b[i]--; G[a[i]].pb({b[i], c[i]}); G[b[i]].pb({a[i], c[i]}); } Dijkstra ds; ds.init(n); ds.dijkstra(0); ll ans = ds.d[n-1]; for (int i = 0; i < n; i++) { G[i].clear(); } int now = n - 1; set

st; while(now != 0){ int to = ds.b[now]; st.insert({now, to}); st.insert({to, now}); now = to; } for (int i = 0; i < m; i++) { if(st.find({a[i], b[i]}) != st.end()){ G[a[i]].pb({b[i], d[i]}); G[b[i]].pb({a[i], d[i]}); } else{ G[a[i]].pb({b[i], c[i]}); G[b[i]].pb({a[i], c[i]}); } } ds.init(n); ds.dijkstra(0); ans += ds.d[n-1]; cout << ans << endl; return 0; }