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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| const int N = 200005; const long long INF = 0x3f3f3f3f3f3f3f3f; struct Edge { int to, nxt; long long len; Edge() { nxt = -1; } }; Edge e[N << 1]; int n, hd[N], pnt, m, q, dfn[2][N], pst[2][N], _time, prever[N], sufver[N]; long long dis[2][N], ans[N], qt[N], qx[N]; bool tr[2][N << 1], vis[N], onRoute[2][N << 1]; priority_queue <pair <long long, int>, vector <pair <long long, int> >, greater <pair <long long, int> > > que; vector <pair <int, long long> > adj[N];
#define Minv(p) (p ? p->mnv : INF) #define Lc(p) (p ? p->l : NULL) #define Rc(p) (p ? p->r : NULL) struct Node { long long mnv; Node *l, *r; inline void Update() { mnv = min(Minv(l), Minv(r)); } inline Node() { mnv = INF; l = r = NULL; } }; Node nd[N * 50]; int top; struct Segtree { Node *_root[N << 1]; inline void Modify(Node *&p1, Node *p2, int pl, int pr, int idx, long long val) { p1 = &nd[++top]; if (p2) nd[top] = *p2; else nd[top] = Node(); if (pl == pr) { p1->mnv = min(p1->mnv, val); return; } int mid = pl + pr >> 1; if (mid >= idx) Modify(p1->l, Lc(p2), pl, mid, idx, val); else Modify(p1->r, Rc(p2), mid + 1, pr, idx, val); p1->Update(); } inline long long Query(Node *p, int pl, int pr, int l, int r) { if (l > r) return INF; if (pl == l && pr == r) return Minv(p); int mid = pl + pr >> 1; if (mid >= r) return Query(Lc(p), pl, mid, l, r); else if (mid + 1 <= l) return Query(Rc(p), mid + 1, pr, l, r); else return min(Query(Lc(p), pl, mid, l, mid), Query(Rc(p), mid + 1, pr, mid + 1, r)); } }; Segtree sgt[2];
inline void AddEdge(int u, int v, long long w) { e[++pnt].to = v; e[pnt].len = w; e[pnt].nxt = hd[u]; hd[u] = pnt; }
inline void Read() { n = qread(); m = qread(); q = qread(); for (int i = 1;i <= m;i++) { int u = qread(), v = qread(), w = qread(); AddEdge(u, v, w); AddEdge(v, u, w); } for (int i = 1;i <= q;i++) { qt[i] = qread(); qx[i] = qread(); } }
inline void Dijkstra(int x) { memset(dis[x], 0x3f, sizeof(dis[x])); if (x) que.push(make_pair(dis[x][n] = 0, n)); else que.push(make_pair(dis[x][1] = 0, 1)); while (!que.empty()) { int u = que.top().second; if (dis[x][u] < que.top().first) { que.pop(); continue; } que.pop(); for (int i = hd[u];~i;i = e[i].nxt) { if (dis[x][e[i].to] > dis[x][u] + e[i].len) { dis[x][e[i].to] = dis[x][u] + e[i].len; que.push(make_pair(dis[x][e[i].to], e[i].to)); } } } memset(vis, 0, sizeof(vis)); for (int i = 1;i <= n;i++) { for (int j = hd[i];~j;j = e[j].nxt) { if (dis[x][e[j].to] == dis[x][i] + e[j].len && !vis[e[j].to]) { tr[x][j] = 1; vis[e[j].to] = 1; } } } }
inline bool Dfs(int x, int u) { dfn[x][u] = ++_time; bool flag = 0; if (x == 0 && u == n) flag = 1; if (x == 1 && u == 1) flag = 1; for (int i = hd[u];~i;i = e[i].nxt) { if (tr[x][i]) { bool res = Dfs(x, e[i].to); onRoute[x][i] = res; flag |= res; } } pst[x][u] = _time; return flag; }
inline void Prefix() { for (int i = 1;i <= pnt;i += 2) { int u = e[i].to, v = e[i + 1].to; if ((onRoute[0][i] && onRoute[1][i + 1]) || (onRoute[0][i + 1] && onRoute[1][i])) continue; int w = e[i].len; adj[dfn[0][u]].push_back(make_pair(dfn[1][v], dis[0][u] + w + dis[1][v])); adj[dfn[0][v]].push_back(make_pair(dfn[1][u], dis[0][v] + w + dis[1][u])); } }
inline void BuildPre() { for (int i = 1;i <= n;i++) { int siz = adj[i].size(); prever[i] = prever[i - 1]; for (int j = 0;j < siz;j++) { sgt[0].Modify(sgt[0]._root[prever[i] + 1], sgt[0]._root[prever[i]], 1, n, adj[i][j].first, adj[i][j].second); prever[i]++; } } }
inline void BuildSuf() { for (int i = n;i >= 1;i--) { int siz = adj[i].size(); sufver[i] = sufver[i + 1]; for (int j = 0;j < siz;j++) { sgt[1].Modify(sgt[1]._root[sufver[i] + 1], sgt[1]._root[sufver[i]], 1, n, adj[i][j].first, adj[i][j].second); sufver[i]++; } } }
inline void Solve() { BuildPre(); for (int i = 1;i <= q;i++) { int t = 2 * qt[i] - 1, x = qx[i], u = e[t].to, v = e[t + 1].to; if (!((onRoute[0][t] && onRoute[1][t + 1]) || (onRoute[1][t] && onRoute[0][t + 1]))) { ans[i] = min(dis[0][n], min(dis[0][u] + x + dis[1][v], dis[1][u] + x + dis[0][v])); continue; } if (dis[0][u] > dis[0][v]) swap(u, v); ans[i] = min(dis[0][u] + x + dis[1][v], dis[1][u] + x + dis[0][v]); ans[i] = min(ans[i], sgt[0].Query(sgt[0]._root[prever[dfn[0][v] - 1]], 1, n, 1, dfn[1][u] - 1)); ans[i] = min(ans[i], sgt[0].Query(sgt[0]._root[prever[dfn[0][v] - 1]], 1, n, pst[1][u] + 1, n)); } top = 0; BuildSuf(); for (int i = 1;i <= q;i++) { int t = 2 * qt[i] - 1, x = qx[i], u = e[t].to, v = e[t + 1].to; if (!((onRoute[0][t] && onRoute[1][t + 1]) || (onRoute[1][t] && onRoute[0][t + 1]))) continue; if (dis[0][u] > dis[0][v]) swap(u, v); ans[i] = min(ans[i], sgt[1].Query(sgt[1]._root[sufver[pst[0][v] + 1]], 1, n, 1, dfn[1][u] - 1)); ans[i] = min(ans[i], sgt[1].Query(sgt[1]._root[sufver[pst[0][v] + 1]], 1, n, pst[1][u] + 1, n)); } for (int i = 1;i <= q;i++) cout << ans[i] << '\n'; }
int main() { std::ios::sync_with_stdio(0); memset(hd, -1, sizeof(hd)); Read(); Dijkstra(0); Dijkstra(1); _time = 0; Dfs(0, 1); _time = 0; Dfs(1, n); Prefix(); Solve(); return 0; }
|