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
| #include <cstdio> #include <vector> #include <queue> #include <algorithm> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef pair <int, int> PII; const int N = 4e5 + 10, M = 20, inf = 2e9; int n, m, q, d[N], p[N], w[N], f[N][M], v[N][M]; vector <int> g[N]; struct Edge { int u, v, w; bool operator < (const Edge &_) const { return w > _.w; } } e[N]; namespace SP { const int M = 8e5 + 10; bool vis[N]; int idx, hd[N], nxt[M], edg[M], wt[M]; void add (int a, int b, int c) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; wt[idx] = c; } void init () { idx = 0; for (int i = 1; i <= n; ++i) hd[i] = -1, vis[i] = false; } void solve () { d[1] = 0; for (int i = 2; i <= n; ++i) d[i] = inf; priority_queue <PII, vector <PII>, greater<PII> > q; q.push(mp(0, 1)); while (!q.empty()) { int t = q.top().se; q.pop(); if (vis[t]) continue; vis[t] = true; for (int i = hd[t]; ~i; i = nxt[i]) if (wt[i] < d[edg[i]] - d[t]) { d[edg[i]] = d[t] + wt[i]; q.push(mp(d[edg[i]], edg[i])); } } } } int fa (int x) { return p[x] == x ? x : p[x] = fa(p[x]); } void dfs (int x) { if (!g[x].empty()) d[x] = inf; for (int i : g[x]) { f[i][0] = x, v[i][0] = w[x]; for (int k = 0; k + 1 < M; ++k) { f[i][k + 1] = f[f[i][k]][k]; v[i][k + 1] = min(v[i][k], v[f[i][k]][k]); } dfs(i); d[x] = min(d[x], d[i]); } } int main () { int T; read(T); while (T--) { read(n, m); for (int i = 1; i <= n; ++i) g[i].clear(), p[i] = i; SP::init(); for (int i = 1, a; i <= m; ++i) { read(e[i].u, e[i].v, a, e[i].w); SP::add(e[i].u, e[i].v, a); SP::add(e[i].v, e[i].u, a); } SP::solve(); sort(e + 1, e + m + 1); int tot = n; for (int i = 1; i <= m; ++i) { int a = fa(e[i].u), b = fa(e[i].v); if (a == b) continue; p[a] = p[b] = ++tot, p[tot] = tot; g[tot].clear(); w[tot] = e[i].w; g[tot].pb(a), g[tot].pb(b); } int rt = fa(1); f[rt][0] = 0, dfs(rt); int S; bool op; read(q, op, S); for (int u, p, last = 0; q; --q) { read(u, p); u = (u + op * last - 1) % n + 1, p = (p + op * last) % (S + 1); for (int i = M - 1; ~i; --i) if (f[u][i] && v[u][i] > p) u = f[u][i]; write(last = d[u]), puts(""); } } return 0; }
|