1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| #include <bits/stdc++.h> using namespace std; #define ll long long const int INF = 0x3f3f3f3f; const int maxn = 2e5 + 10; struct LCT { #define lc c[x][0] #define rc c[x][1] int v[maxn], f[maxn], c[maxn][2], siz[maxn], mx[maxn]; bool r[maxn]; inline bool nroot(int x) { return c[f[x]][0] == x || c[f[x]][1] == x; } inline void pushup(int x) { mx[x] = x; if (lc&&v[mx[lc]] > v[mx[x]])mx[x] = mx[lc]; if (rc&&v[mx[rc]] > v[mx[x]])mx[x] = mx[rc]; } inline void rev(int x) { swap(lc, rc); r[x] ^= 1; } inline void pushdown(int x) { if (r[x]) { if (lc)rev(lc); if (rc)rev(rc); r[x] = 0; } } inline void update(int x) { siz[x] = siz[lc] + siz[rc]; } inline void rotate(int x) { int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][k ^ 1]; if (nroot(y))c[z][c[z][1] == y] = x; if (w)f[w] = y; c[x][k ^ 1] = y; c[y][k] = w; f[y] = x; f[x] = z; pushup(y); update(y), update(x); } inline void pushall(int x) { if (nroot(x))pushall(f[x]); pushdown(x); } inline void splay(int x) { pushall(x); int y, z; while (nroot(x)) { y = f[x]; z = f[y]; if (nroot(y))rotate((c[y][0] == x && c[z][0] == y ? y : x)); rotate(x); } pushup(x); } inline void access(int x) { for (int y = 0; x; y = x, x = f[x]) splay(x), rc = y, pushup(x); } inline void makeroot(int x) { access(x); splay(x); rev(x); } int findroot(int x) { access(x); splay(x); while (lc)pushdown(x), x = lc; splay(x); return x; } inline void split(int x, int y) { makeroot(x); access(y); splay(y); } inline void link(int x, int y) { makeroot(x); if (findroot(y) != x)f[x] = y; } inline void cut(int x, int y) { makeroot(x); if (findroot(y) == x && f[y] == x && !c[y][0]) { f[y] = c[x][1] = 0; pushup(x); } } int query(int x, int y) { makeroot(x); access(y); splay(y); return mx[y]; } }lct; int n, m; int p[maxn], rk[maxn]; int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } void merge(int x, int y) { x = find(x), y = find(y); if (x == y)return; if (rk[x] < rk[y])p[x] = y; else { p[y] = x; if (rk[x] == rk[y])rk[x]++; } } struct E { int u, v, a, b; bool operator<(const E& t)const { return a < t.a; } }es[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++)p[i] = i; for (int i = 1; i <= m; i++) { int u, v, a, b; scanf("%d%d%d%d", &es[i].u, &es[i].v, &es[i].a, &es[i].b); } sort(es + 1, es + m + 1); int ans = INF; for (int i = 1; i <= m; i++) { int u = es[i].u, v = es[i].v, a = es[i].a, b = es[i].b; if (find(u) == find(v)) { int t = lct.query(u, v); if (lct.v[t] > b) { lct.cut(t, es[t - n].u); lct.cut(t, es[t - n].v); } else { if (find(1) == find(n))ans = min(ans, a + lct.v[lct.query(1, n)]); continue; } } else { merge(u, v); } lct.v[i + n] = b; lct.mx[i + n] = i + n; lct.link(u, i + n); lct.link(v, i + n); if (find(1) == find(n))ans = min(ans, a + lct.v[lct.query(1, n)]); } if (ans == INF)puts("-1"); else printf("%d\n", ans); return 0; }
|