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
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <cctype> #include <vector> #include <cmath> #include <queue> using namespace std; #define int long long #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define pii pair<int,int> #define mp make_pair
char buf[1 << 20], *p1, *p2; #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)?EOF: *p1++) template <typename T> inline void read(T &t) { int v = getchar();T f = 1;t = 0; while (!isdigit(v)) {if (v == '-')f = -1;v = getchar();} while (isdigit(v)) {t = t * 10 + v - 48;v = getchar();} t *= f; } template <typename T,typename... Args> inline void read(T &t,Args&... args) { read(t);read(args...); }
const int N = 1e6 + 10; const int K = 26;
int n,m,q,k,s,dis[N],F[N],poi[N],val[N],tot,fat[N][K],f[N][K],dep[N]; bool vis[N]; vector <int> G[N]; vector <pii> G2[N];
struct edg { int u,v,l,a; friend inline bool operator < (const edg &A,const edg &B) { return A.a > B.a; } }edge[N];
struct node { int pos,d; friend inline bool operator < (const node &a,const node &b) { return a.d > b.d; } };
int find(int x) {return x == F[x] ? x : F[x] = find(F[x]);}
void clear() { tot = n; for (int i = 1;i <= (n << 1);++i) G[i].clear(),G2[i].clear(),F[i] = i; }
void Dijkstra(int s) { priority_queue <node> q; memset(vis,0,sizeof vis); memset(dis,0x3f,sizeof dis); memset(val,0x3f,sizeof val); q.push((node){s,dis[s] = 0});
while (!q.empty()) { node p = q.top();q.pop(); int x = p.pos; if (vis[x]) continue; vis[x] = 1; for (auto e : G2[x]) { int y = e.first,w = e.second; if (dis[y] > dis[x] + w) { dis[y] = dis[x] + w; q.push((node){y,dis[y]}); } } } for (int i = 1;i <= n;++i) val[i] = dis[i]; }
void Kruskal() { int cnt = 0; for (int i = 1;i <= m;++i) { int u = edge[i].u,v = edge[i].v,w = edge[i].a; u = find(u),v = find(v); if (u != v) { ++tot;poi[tot] = w; F[u] = F[v] = tot; val[tot] = 0x3f3f3f3f; G[tot].push_back(u); G[u].push_back(tot); G[tot].push_back(v); G[v].push_back(tot); if (++cnt == (n - 1)) break; } } }
void dfs(int x,int fa) { fat[x][0] = fa; dep[x] = dep[fa] + 1; for (int j = 1;j <= 20;++j) { fat[x][j] = fat[fat[x][j-1]][j-1]; } for (auto y : G[x]) { if (y != fa) { dfs(y,x); val[x] = min(val[x],val[y]); } } }
int query(int x,int y) { for (int i = 20;i >= 0;--i) { if (fat[x][i]) { if (poi[fat[x][i]] > y) { x = fat[x][i]; } } } return val[x]; }
void solve() { read(n,m); clear(); for (int i = 1;i <= m;++i) { read(edge[i].u,edge[i].v,edge[i].l,edge[i].a); G2[edge[i].u].push_back(mp(edge[i].v,edge[i].l)); G2[edge[i].v].push_back(mp(edge[i].u,edge[i].l)); } sort(edge + 1,edge + 1 + m); Dijkstra(1); Kruskal(); dfs(tot,0); read(q,k,s); int lastans = 0; for (int i = 1;i <= q;++i) { int x,p;read(x,p); x = (x + k * lastans - 1) % n + 1; p = (p + k * lastans) % (s + 1); printf("%lld\n",lastans = query(x,p)); }
}
signed main() { int T;read(T); while (T--) solve(); return 0; }
|