#include #define rep(i,n) for(int i=0;i<(int)(n);i++) #define chmin(x,y) x = min((x),(y)); #define chmax(x,y) x = max((x),(y)); using namespace std; using ll = long long ; using P = pair ; using pll = pair; const int INF = 1e9; const long long LINF = 1e17; const int MOD = 1000000007; //const int MOD = 998244353; const double PI = 3.14159265358979323846; int n = 100005; int m; vector> graph(n); using p = pair; vector before(n); vector Dijkstra(int start){ vector seen(n,false); vector shortest(n,LINF); priority_queue,greater

> pq; pq.push(p(pll{0,start},-1)); while(!pq.empty()){ ll dist = pq.top().first.first; int node = pq.top().first.second; int b = pq.top().second; pq.pop(); if(seen[node]) continue; shortest[node] = dist; before[node] = b; seen[node] = true; for(auto next:graph[node]){ int next_node = next.first; ll next_cost = next.second; if(seen[next_node]) continue; pq.push(p(pll{next_cost + dist,next_node},node)); } } return shortest; } int main(){ cin >> n >> m; map dist; rep(i,m){ int u,v,c,d; cin >> u >> v >> c >> d; --u;--v; graph[u][v] = c; graph[v][u] = c; if(u>v) swap(u,v); dist[P(u,v)] = d; } //rep(i,n){ //for(auto p:graph[i]) //} vector res = Dijkstra(0); //rep(i,n) cout << res[i] << " "; //cout << endl; int now = n-1; while(now > 0){ int a = now; int b = before[now]; graph[a][b] = dist[P(min(a,b),max(a,b))]; graph[b][a] = graph[a][b]; now = b; //cout << now << endl; } vector ret = Dijkstra(n-1); cout << res[n-1] + ret[0] << endl; return 0; }