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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include <bits/stdc++.h> using namespace std; namespace Legendgod { namespace Read {
#ifdef Fread const int Siz = (1 << 21) + 5; char *iS, *iT, buf[Siz]; #define gc() ( iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iS == iT ? EOF : *iS ++) : *iS ++ ) #define getchar gc #endif template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; !isdigit(c); c = getchar()) if(c == '-') f = -1; for(; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= f; } template <typename T, typename...Args> void r1(T &x, Args&...arg) { r1(x), r1(arg...); } #undef getchar }
using namespace Read;
const int maxn = 1e3 + 5; int n, m; int head[maxn], cnt(1); struct Edge { int to, next, w; }edg[maxn * maxn]; void add(int u,int v,int w) { edg[++ cnt] = (Edge) {v, head[u], w}, head[u] = cnt; }
struct Node { int id, dis; Node(int a = 0,int b = 0) : id(a), dis(b) {} int operator < (const Node &z) const { return dis > z.dis; } };
bitset<maxn> vis; int pre[maxn]; void Dj(int st, int *d) { static priority_queue<Node> q; while(!q.empty()) q.pop(); for(int i = 1; i <= n; ++ i) d[i] = 0x3f3f3f3f, pre[i] = 0; vis.reset(); d[st] = 0; q.push(Node(st, 0)); while(!q.empty()) { int u = q.top().id; q.pop(); if(vis[u]) continue; vis[u] = 1; for(int i = head[u];i;i = edg[i].next) { int to = edg[i].to, w = edg[i].w; if(d[u] + w < d[to]) { d[to] = d[u] + w; pre[to] = u; if(!vis[to]) q.push(Node(to, d[to])); } } } }
int d[2][maxn]; const int maxm = (maxn << 2); struct Seg { int t[maxm], tag[maxm], md[maxm]; #define ls (p << 1) #define rs (p << 1 | 1) #define mid md[p] void build(int p,int l,int r) { tag[p] = 0x3f3f3f3f, t[p] = 0x3f3f3f3f; if(l == r) return ; md[p] = (l + r) >> 1; build(ls, l, mid), build(rs, mid + 1, r); } void padd(int p,int c) { t[p] = min(t[p], c), tag[p] = min(tag[p], c); } void pushdown(int p) { if(tag[p] != 0x3f3f3f3f) { padd(ls, tag[p]), padd(rs, tag[p]); tag[p] = 0x3f3f3f3f; } } void pushup(int p) { t[p] = min(t[ls], t[rs]); } void Add(int p,int l,int r,int ll,int rr,int c) { if(ll > rr) return ; if(ll <= l && r <= rr) return padd(p, c); pushdown(p); if(ll <= mid) Add(ls, l, mid, ll, rr, c); if(mid < rr) Add(rs, mid + 1, r, ll, rr, c); pushup(p); } int Ask(int p,int l,int r,int pos) { if(l == r) return t[p]; pushdown(p); if(pos <= mid) return Ask(ls, l, mid, pos); else return Ask(rs, mid + 1, r, pos); } }T;
int pos[maxn], fa[maxn], vc[maxn][maxn];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
signed main() { int i, j; memset(vc, 0x3f, sizeof(vc)); r1(n, m); for(i = 1; i <= m; ++ i) { int u, v, w; r1(u, v, w); add(u, v, w), add(v, u, w); vc[u][v] = vc[v][u] = w; } Dj(n, d[1]); Dj(1, d[0]); for(i = 1; i <= n; ++ i) fa[i] = pre[i]; m = 0; for(i = n; i; i = pre[i]) { pos[i] = ++ m; fa[i] = i;
if(pre[i]) vc[i][pre[i]] = vc[pre[i]][i] = 0x3f3f3f3f; }
T.build(1, 1, m); for(i = 1; i <= n; ++ i) { for(j = i + 1; j <= n; ++ j) if(vc[i][j] != 0x3f3f3f3f) { int c = min(d[0][i] + vc[i][j] + d[1][j], d[1][i] + vc[i][j] + d[0][j]); int L = pos[getfa(i)], R = pos[getfa(j)]; if(L > R) swap(L, R);
T.Add(1, 1, m, L + 1, R, c); } } int ans(0); for(i = 2; i <= m; ++ i) ans = max(ans, T.Ask(1, 1, m, i)); printf("%d\n", ans); return 0; }
}
signed main() { return Legendgod::main(), 0; }
|