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
| #include <bits/stdc++.h> using namespace std;
#define Fread
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
const int maxn = 8e5 + 5; const int maxm = maxn << 1; const int N = 23;
int n, m;
int head[maxn], f[maxn][25]; int fa[maxn], cnt(0); struct Edge { int to, next, w; }edg[maxn << 1]; void add(int u,int v,int w) { edg[++ cnt] = (Edge) {v, head[u], w}, head[u] = cnt; }
priority_queue<pair<int, int> > q; int d[maxn], vis[maxn]; void DJ() { for(int i = 1; i <= n; ++ i) vis[i] = 0, d[i] = 0x3f3f3f3f; d[1] = 0; q.push(make_pair(0, 1)); while(!q.empty()) { int u = q.top().second; q.pop(); if(vis[u]) continue; vis[u] = 1; for(int i = head[u];i;i = edg[i].next) { int to = edg[i].to; if(d[to] > d[u] + edg[i].w) { d[to] = d[u] + edg[i].w; if(!vis[to]) q.push(make_pair(-d[to], to)); } } } }
struct Edgee { int u, v, a; int operator < (const Edgee &z) const { return a > z.a; } }E[maxn]; struct Node { int l, a; }t[maxn << 1];
int Q, K, S; int lastans(0); int getv() { int v; r1(v); return (v + K * lastans - 1) % n + 1; } int getp() { int p; r1(p); return (p + K * lastans) % (S + 1); }
vector<int> vc[maxn];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); } int dep[maxn]; void dfs(int p,int pre) { f[p][0] = pre; dep[p] = dep[pre] + 1; for(int i = 1; i <= N; ++ i) f[p][i] = f[f[p][i - 1]][i - 1]; for(vector<int> :: iterator it = vc[p].begin(); it != vc[p].end(); ++ it) { int to = *it; dfs(to, p); t[p].l = min(t[p].l, t[to].l); } }
int query(int v,int p) { for(int i = N; ~ i; -- i) { if(dep[v] - (1 << i) > 0) { if(t[f[v][i]].a > p) v = f[v][i]; } } return t[v].l; }
void Kruscal() { int i; sort(E + 1, E + m + 1); for(i = 1; i <= (n << 1); ++ i) fa[i] = i; int tot(0); int ct(n); for(i = 1; i <= m; ++ i) { int u = E[i].u, v = E[i].v; int u1 = getfa(u), v1 = getfa(v); if(u1 == v1) continue; ++ ct; vc[ct].push_back(u1); vc[ct].push_back(v1); fa[u1] = fa[v1] = ct; t[ct].a = E[i].a; ++ tot; if(tot == n - 1) break; } dfs(ct, 0); }
signed main() {
int i, j, T(1); r1(T); while(T --) { r1(n, m); for(i = 1; i <= m; ++ i) { int u, v, l, a; r1(u, v, l, a); E[i] = (Edgee) {u, v, a}; add(u, v, l), add(v, u, l); } lastans = 0; DJ(); for(i = 1; i <= (n << 1); ++ i) t[i].l = 2e9; for(i = 1; i <= n; ++ i) t[i].l = d[i]; r1(Q, K, S); Kruscal(); for(int _ = 1; _ <= Q; ++ _) { int v(getv()); int p(getp()); printf("%d\n", lastans = query(v, p)); }
cnt = 0; for(i = 1; i <= (n << 1); ++ i) fa[i] = head[i] = 0, vc[i].clear(), t[i].a = 0; for(i = 1; i <= (n << 1); ++ i) { for(j = 0; j <= N; ++ j) f[i][j] = 0; } } return 0; }
|