/* author: Kite_kuma created: 2020.10.30 23:12:57 */ // #ifdef LOCAL // #define _GLIBCXX_DEBUG // #endif #include using namespace std; #pragma region aliases #define rep(i, n) for(long long i = 0; i < (n); i++) #define rrep(i, n) for(long long i = (n)-1; i > -1; i--) #define Rep(i, m, n) for(long long i = (m); i < (n); i++) #define rRep(i, m, n) for(long long i = (n)-1; i >= (m); i--) #define REP(i, m, n, p) for(long long i = m; i < n; i += p) #define foa(s, v) for(auto &s : v) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define bcnt(n) __builtin_popcountll(n) #define endk endl #define ednl endl #define enld endl using ll = long long; using ld = long double; using ull = unsigned long long; using vb = vector; using vi = vector; using vvi = vector>; using vvvi = vector>>; using vll = vector; using vvll = vector; using vvvll = vector; using mll = map; using pll = pair; using qll = queue; using sll = set; using vpll = vector>; template using V = vector; template using VV = V>; template using VVV = V>>; //昇順pq(小さい方から取り出す) template using pqup = priority_queue, greater>; //降順pq(大きい方から取り出す) template using pqdn = priority_queue; #pragma endregion #pragma region constants long long const limLL = 9223372036854775807; // POW(2,63)-1 ~ 9.22e18 long long const dekai = 3e16; const long double pi = acos(-1); const char dl = '\n'; int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; const int mod = 1000000007; // const int mod = 998244353; #pragma endregion #pragma region basic_procedure template inline bool isin(T x, T lef, T rig) { return ((lef <= x) && (x < rig)); } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } void Yes(bool f = 1) { cout << (f ? "Yes" : "No") << "\n"; } void No() { cout << "No\n"; } void YES(bool f = 1) { cout << (f ? "YES" : "NO") << "\n"; } void NO() { cout << "NO\n"; } template void drop(T answer) { cout << answer << "\n"; exit(0); } void err() { cout << -1 << "\n"; exit(0); } template void vout(vector &v, bool tate = 0) { if(v.size() > 0) { for(auto it = v.begin(); it < v.end(); it++) { cout << *it; if(it != v.end() - 1) { if(tate) cout << endl; else cout << " "; } } } cout << endl; } template void add(vector &v, T val) { //ベクトルの各要素に加算 for(auto &a : v) a += val; return; } // vectorの中身を数える map<要素,個数>を返す template map cntv(vector v) { map m; for(auto &g : v) { if(m.count(g)) m[g]++; else m[g] = 1; } return m; } //配列圧縮 //{1,36,1,3,8,-2,-92}を //{2, 5,2,3,4, 1, 0}にする template vector press(vector &v) { int n = v.size(); vector w(n); map m; for(T &p : v) m[p] = 0; int i = 0; for(auto &p : m) { p.second = i; i++; } for(int i = 0; i < n; i++) w.at(i) = m[v.at(i)]; return w; } // 切り上げ除算 template T dup(T a, T b) { assert(b != 0); T x = abs(a); T y = abs(b); T z = (x + y - 1) / y; if((a < 0 && b > 0) || (a > 0 && b < 0)) return -z; else if(a == 0) return 0; else return z; } long long POW(long long a, long long n) { long long res = 1; while(n > 0) { if(n & 1) res = res * a; a = a * a; n >>= 1; } return res; } template T greatest_lower_multiple(T x, T d) { if(d == 0) return 0; if(d < 0) d *= -1; T y = x % d; if(y < 0) y += d; return x - y; } template T least_upper_multiple(T x, T d) { return -greatest_lower_multiple(-x, d); } long long modpow(long long a, long long n, long long mod) { // a^n mod assert(n >= 0); if(mod == 1) return 0LL; long long res = 1; while(n > 0) { if(n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a * x % mod == __gcd(a,mod)なるxを返す // a が modの倍数でないことが条件 long long modinv(long long a, long long mod) { long long b = mod, u = 1, v = 0; while(b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if(u < 0) u += mod; return u; } vvll comb(100, vll(100, -1)); long long com(long long n, long long k) { //普通の二項計数(overflowに注意) assert(n < 100 && k < 100); if(n < k || k < 0 || n < 0) return 0; if(comb[n][k] != -1) return comb[n][k]; ll res; if(n - k < k) res = com(n, n - k); else if(k == 0) res = 1; else res = com(n - 1, k - 1) + com(n - 1, k); comb[n][k] = res; return res; } // nCk modを求める const int MAX = 5100000; // この値は求める二項計数の値に応じて変える // MAX=3*10^7のとき1900msほど、ほぼ比例 // MAX=5*10^6程度ならそれほど気にしなくてよい(300ms程) long long fac[MAX], finv[MAX], inv[MAX]; void cominit() { // テーブルを作る前処理 fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } long long commod(long long n, long long k) { // 二項係数 if(n < k) return 0; if(n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } long long pmod(long long n, long long k) { // 順列 if(n < k) return 0; if(n < 0 || k < 0) return 0; return fac[n] * finv[n - k] % mod; } // n個の区別しないボールを区別するk個の箱に入れる方法の総数 long long hmod(long long n, long long k) { // 重複組み合わせ return commod(n + k - 1, n); } #pragma endregion #pragma region input #define VEC(type, name, size) \ vector name(size); \ INPUT(name) #define VVEC(type, name, h, w) \ vector> name(h, vector(w)); \ INPUT(name) #define INT(...) \ int __VA_ARGS__; \ INPUT(__VA_ARGS__) #define LL(...) \ long long __VA_ARGS__; \ INPUT(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ INPUT(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ INPUT(__VA_ARGS__) #define DOUBLE(...) \ double __VA_ARGS__; \ INPUT(__VA_ARGS__) #define LD(...) \ long double __VA_ARGS__; \ INPUT(__VA_ARGS__) template void scan(T &a) { cin >> a; } template void scan(vector &a) { for(auto &i : a) scan(i); } template void scan(pair &p) { scan(p.first); scan(p.second); } void INPUT() {} template void INPUT(Head &head, Tail &... tail) { scan(head); INPUT(tail...); } template inline void print(T x) { cout << x << '\n'; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } #pragma endregion #pragma region debug #pragma region output template ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template ostream &operator<<(ostream &os, const vector &v) { for(int i = 0; i < (int)v.size(); i++) { if(i) os << " "; os << v[i]; } return os; } #pragma endregion #pragma region view template void view(const T e) { std::cerr << e; } template void view(const std::pair &p) { std::cerr << "("; view(p.first); cerr << ", "; view(p.second); cerr << ")"; } template void view(const std::set &s) { if(s.empty()) { cerr << "{ }"; return; } std::cerr << "{ "; for(auto &t : s) { view(t); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector &v) { if(v.empty()) { cerr << "{ }"; return; } std::cerr << "{ "; for(const auto &e : v) { view(e); std::cerr << ", "; } std::cerr << "\b\b }"; } template void view(const std::vector> &vv) { std::cerr << "{\n"; for(const auto &v : vv) { std::cerr << "\t"; view(v); std::cerr << "\n"; } std::cerr << "}"; } template void view(const std::vector> &v) { std::cerr << "{\n"; for(const auto &c : v) { std::cerr << "\t("; view(c.first); cerr << ", "; view(c.second); cerr << ")\n"; } std::cerr << "}"; } template void view(const std::map &m) { std::cerr << "{\n"; for(const auto &t : m) { std::cerr << "\t["; view(t.first); cerr << "] : "; view(t.second); cerr << "\n"; } std::cerr << "}"; } #pragma endregion // when debugging : g++ foo.cpp -DLOCAL #ifdef LOCAL void debug_out() {} template void debug_out(Head H, Tail... T) { view(H); cerr << ", "; debug_out(T...); } #define debug(...) \ do { \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \ debug_out(__VA_ARGS__); \ cerr << "\b\b]\n"; \ } while(0) #define dump(x) \ do { \ cerr << __LINE__ << " " << #x << " : "; \ view(x); \ cerr << "\n"; \ } while(0) #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #pragma endregion #pragma region graph_structure template struct edge { T len; int from; int to; bool operator<(const edge a) const { if(len != a.len) return len < a.len; if(from != a.from) return from < a.from; return to < a.to; } bool operator>(const edge a) const { if(len != a.len) return len > a.len; if(from != a.from) return from > a.from; return to > a.to; } }; template struct graph { // 0-indexed T const INF = numeric_limits::max() / 3; vector>> edges; int ver; // constructor graph() = default; graph(int vertex) : ver(vertex), edges(vertex) {} //辺の追加 (0-indexed) void update(int from, int to, T len = 1, bool direction = 1) { // checked edge e; e.len = len; e.from = from; e.to = to; edges[from].push_back(e); if(!direction) { swap(e.to, e.from); edges[to].push_back(e); } } //入力受取 (1-indexed) void input(int edge_num, bool direction = false, bool weight = false, int index = 1) { // checked for(int i = 0; i < edge_num; i++) { int a; int b; cin >> a >> b; a -= index; b -= index; T c; if(weight) cin >> c; else c = 1; update(a, b, c, direction); } } // 辺の長さを全て1とみたときの単一始点最短経路 (無理なときはINF) vector bfs(int start) { // checked // https://atcoder.jp/contests/abc007/submissions/me vector ret(ver, INF); queue q; q.push(start); ret[start] = 0; while(!q.empty()) { int now = q.front(); q.pop(); for(auto &e : edges[now]) { if(ret[e.to] != INF) continue; q.push(e.to); ret[e.to] = ret[now] + 1; } } return ret; } //長さが負のpathがないときの単一始点最短経路 O((ver)log(ver)+(edge)) vector dijkstra(int start) { // checked // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_1_A/judge/4817136/C++14 vector ret(ver, (T)INF); //{dist,place} priority_queue, vector>, greater>> p; //{dist,place} p.push({0, start}); ret[start] = 0; while(!p.empty()) { T dist = p.top().first; int place = p.top().second; p.pop(); if(ret[place] < dist) continue; for(auto &next : edges[place]) { int nextplace = next.to; T dis = next.len; if(ret[nextplace] > dist + dis) { ret[nextplace] = dist + dis; p.push({ret[nextplace], nextplace}); } } } return ret; } //単一始点最短経路 O((ver)*(edge)) //辿り着けないとき ret[i] = INF; //ある頂点までのコストが無限に小さくなり得るとき→ ret[i] = -INF; vector BellmanFord(int start) { // checked // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_1_B/judge/4817139/C++14 vector ret(ver, INF); ret[start] = 0; for(int loop = 0; loop < ver - 1; loop++) { for(int v = 0; v < ver; v++) { if(ret[v] == INF) continue; for(auto &e : edges[v]) { ret[e.to] = min(ret[e.to], ret[v] + e.len); } } } //無限降下点の検索 queue q; vector chk(ver, 0); for(int v = 0; v < ver; v++) { if(ret[v] == INF) continue; for(auto &e : edges[v]) { if(ret[e.to] > ret[v] + e.len) { ret[e.to] = ret[v] + e.len; if(!chk[e.to]) { q.push(e.to); chk[e.to] = 1; } } } } while(!q.empty()) { int now = q.front(); q.pop(); for(auto &e : edges[now]) { if(!chk[e.to]) { chk[e.to] = 1; q.push(e.to); } } } for(int i = 0; i < ver; i++) if(chk[i]) ret[i] = -INF; return ret; } //閉路に含まれない頂点列挙 //要素数がver未満なら閉路が存在、そうでなければ閉路は存在しない vector topo_sort() { // checked // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_4_B/judge/4817106/C++14 // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_4_A/judge/4817110/C++14 vector num_input(ver); // 入次数 for(int i = 0; i < ver; i++) { for(auto e : edges[i]) { num_input[e.to]++; } } // 入次数が0のノードをqueueで管理する queue que; for(int i = 0; i < ver; i++) { if(num_input[i] == 0) { que.push(i); } } vector ans; while(!que.empty()) { auto node = que.front(); que.pop(); ans.push_back(node); // 頂点の削除 for(auto e : edges[node]) { num_input[e.to]--; // 行き先の入次数が0になったらqueueに追加 if(num_input[e.to] == 0) { que.push(e.to); } } } return ans; } //{{端点、端点},直径の大きさ} pair, T> DiameterOfTree(bool weigh = true) { // chkecked // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_5_A/judge/4817099/C++14 vector vec; vec = weigh ? dijkstra(0) : bfs(0); int v1 = -1; T dia = -1; for(int i = 0; i < ver; i++) if((dia < vec[i])) { dia = vec[i]; v1 = i; } vec = weigh ? dijkstra(v1) : bfs(v1); dia = -1; int v2 = -1; for(int i = 0; i < ver; i++) if((dia < vec[i])) { v2 = i; dia = vec[i]; } pair, T> ans = {{v1, v2}, dia}; return ans; } //無向木構造を根から葉に伸びる有向木構造に書き換える graph RootToLeaf(int root) { // 0-indexed graph ret(ver); vector chk(ver, 0); chk[root] = 1; function dfs = [&](int now) { for(auto &e : edges[now]) { if(chk[e.to] == 1) continue; chk[e.to] = 1; ret.update(now, e.to, e.len); dfs(e.to); } }; dfs(root); return ret; } //無向木構造を葉から根に伸びる有向木構造に書き換える graph LeafToRoot(int root) { // 0-indexed graph ret(ver); vector chk(ver, 0); chk[root] = 1; function dfs = [&](int now) { for(auto &e : edges[now]) { if(chk[e.to] == 1) continue; chk[e.to] = 1; ret.update(e.to, now, e.len); dfs(e.to); } }; dfs(root); ret.update(root, root, 0); return ret; } // LeafToRootのvector版.par[i]=iの親の頂点 vector par(int root) { // 0-indexed vector ret(ver, -1); ret[root] = root; // rootの親はroot function dfs = [&](int now) { for(auto &e : edges[now]) { if(ret[e.to] != -1) continue; ret[e.to] = now; dfs(e.to); } }; dfs(root); return ret; } vector> ParentAndDistance(int root) { // 0-indexed vector> ret(ver); for(int i = 0; i < ver; i++) ret[i].to = -1; ret[root].to = root; // rootの親はroot ret[root].len = 0; // rootの親との距離は0 function dfs = [&](int now) { for(auto &e : edges[now]) { if(ret[e.to].to != -1) continue; ret[e.to].to = now; ret[e.to].len = e.len; dfs(e.to); } }; dfs(root); return ret; } //隣接sheet.主にwarshall用 vector> GraphArray(void) { // chkecked // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_1_C/judge/4817090/C++14 vector> ret(ver, vector(ver, INF)); for(int from = 0; from < ver; from++) { for(auto &e : edges[from]) { ret[from][e.to] = e.len; } ret[from][from] = 0; } return ret; } graph Prim(bool direction = 0, int start = 0) { // checked // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_2_A/judge/4817129/C++14 graph ret(ver); priority_queue, vector>, greater>> p; for(auto &e : edges[start]) { p.push(e); } vector chk(ver, 0); chk[start] = 1; while(!p.empty()) { auto ed = p.top(); p.pop(); if(chk[ed.to]) continue; chk[ed.to] = 1; ret.update(ed.from, ed.to, ed.len, direction); for(auto &e : edges[ed.to]) { p.push(e); } } return ret; } //各頂点を根としたときの木の高さ vector height(int start = 0) { // checked // https://onlinejudge.u-aizu.ac.jp/status/users/Kite_kuma/submissions/1/GRL_5_B/judge/4817082/C++14 vector fir(ver, -1), sec(ver, -1); function dfs = [&](int now, int par) { T f = 0, s = 0; // startを根としたときのnowからの深さ1番目、2番目 for(auto &e : edges[now]) { if(e.to == par) continue; s = max(s, dfs(e.to, now) + e.len); if(f < s) swap(f, s); } sec[now] = s; return fir[now] = f; }; dfs(start, -1); function sol = [&](int now, int par, T parf, T pars, T parlen) { if(fir[now] + parlen == parf) parf = pars; sec[now] = max(sec[now], parf + parlen); if(fir[now] < sec[now]) swap(fir[now], sec[now]); for(auto &e : edges[now]) { if(e.to == par) continue; sol(e.to, now, fir[now], sec[now], e.len); } return; }; sol(start, -1, -1, -1, -1); return fir; } //全方位木DP //マージ関数、上に送るための関数、単位元、はじめの根 // 関数はstd::functionで渡す template vector ReRootingDP(U &unit, function &merge, function &send, int root = 0) { auto tr = RootToLeaf(root); vector> v(ver); vector ret(ver); //求める答 function treeDP = [&](int now) { U res = unit; vector vec; for(auto &e : tr.edges[now]) { U u = send(e.to, treeDP(e.to), e.len); v[now].push_back(u); res = merge(res, u); } return res; }; treeDP(root); function rerooting = [&](int now, U ans_par, int par, T len) { if(now != root) ans_par = send(par, ans_par, len); int sz = v[now].size(); vector data_front(sz + 1), data_back(sz + 1); data_front[0] = data_back[sz] = unit; for(int i = 0; i < sz; i++) { data_front[i + 1] = merge(data_front[i], v[now][i]); data_back[sz - i - 1] = merge(v[now][sz - i - 1], data_back[sz - i]); } for(int i = 0; i < sz; i++) { auto nxtans = merge(ans_par, merge(data_front[i], data_back[i + 1])); rerooting(tr.edges[now][i].to, nxtans, now, tr.edges[now][i].len); } ret[now] = merge(ans_par, data_front[sz]); return; }; rerooting(root, unit, root, 0); return ret; } // HL分解 }; #pragma endregion int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); srand((unsigned)time(NULL)); INT(n, m); graph<> gra(n); map, pair> ma; map, ll> ed; rep(i, m) { INT(u, v); LL(c, d); u--, v--; gra.update(u, v, c, false); ed[make_pair(u, v)] = c; ed[make_pair(v, u)] = c; ma[make_pair(u, v)] = {c, d}; ma[make_pair(v, u)] = {c, d}; } auto res = gra.dijkstra(0); int now = n - 1; dump(ed); dump(ma); while(now) { foa(e, gra.edges[now]) { if(res[e.to] + e.len == res[now]) { ed[make_pair(now, e.to)] = ed[make_pair(e.to, now)] = ma[make_pair(e.to, now)].second; now = e.to; break; } } } dump(ed); dump(ma); graph<> fra(n); foa(t, ed) { fra.update(t.first.first, t.first.second, ed[make_pair(t.first.first, t.first.second)]); } auto ans = fra.dijkstra(0); dump(res); dump(ans); drop(res[n - 1] + ans[n - 1]); return 0; }