#include typedef struct { int key, id; } data; typedef struct { data obj[400001]; int size; } min_heap; void push(data x, min_heap* h) { int i = ++(h->size), j = i >> 1; data tmp; h->obj[i] = x; while (j > 0) { if (h->obj[i].key < h->obj[j].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j >>= 1; } else break; } } data pop(min_heap* h) { int i = 1, j = 2; data output = h->obj[1], tmp; h->obj[1] = h->obj[(h->size)--]; while (j <= h->size) { if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1; if (h->obj[j].key < h->obj[i].key) { tmp = h->obj[j]; h->obj[j] = h->obj[i]; h->obj[i] = tmp; i = j; j = i << 1; } else break; } return output; } typedef struct List { struct List *next; int v, id; long long cost; } list; int main() { int i, N, M, u, w, c, d; list *adj[100001] = {}, e[400001], *p; scanf("%d %d", &N, &M); for (i = 0; i < M; i++) { scanf("%d %d %d %d", &u, &w, &c, &d); e[i*4].v = w; e[i*4+1].v = u; e[i*4+2].v = w; e[i*4+3].v = u; e[i*4].cost = c; e[i*4+1].cost = c; e[i*4+2].cost = d; e[i*4+3].cost = d; e[i*4].id = i * 4; e[i*4+1].id = i * 4 + 1; e[i*4+2].id = i * 4 + 2; e[i*4+3].id = i * 4 + 3; e[i*4].next = adj[u]; e[i*4+1].next = adj[w]; adj[u] = &(e[i*4]); adj[w] = &(e[i*4+1]); e[i*4+2].next = adj[u]; e[i*4+3].next = adj[w]; adj[u] = &(e[i*4+2]); adj[w] = &(e[i*4+3]); } int prev[2][100001], flag[2][100001] = {}; long long dist[2][100001]; for (i = 1; i <= N; i++) { dist[0][i] = 1LL << 60; dist[1][i] = 1LL << 60; prev[0][i] = -1; prev[1][i] = -1; } data da; min_heap h; h.size = 0; dist[0][1] = 0; da.key = 0; da.id = 1; push(da, &h); while (h.size > 0) { u = pop(&h).id; if (flag[0][u] != 0) continue; flag[0][u] = 0; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (dist[0][u] + p->cost < dist[0][w]) { dist[0][w] = dist[0][u] + p->cost; prev[0][w] = p->id; da.key = dist[0][w]; da.id = w; push(da, &h); } } } for (i = prev[0][N]; i != -1; i = prev[0][e[i^1].v]) { e[i].cost = 1LL << 60; e[i^1].cost = 1LL << 60; } dist[1][N] = 0; da.key = 0; da.id = N; push(da, &h); while (h.size > 0) { u = pop(&h).id; if (flag[1][u] != 0) continue; flag[1][u] = 1; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (dist[1][u] + p->cost < dist[1][w]) { dist[1][w] = dist[1][u] + p->cost; prev[1][w] = p->id; da.key = dist[1][w]; da.id = w; push(da, &h); } } } printf("%lld\n", dist[0][N] + dist[1][1]); fflush(stdout); return 0; }