#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 400010;
int T, n, m, q, K, S, c, lastans, dist[maxn], fa[maxn];
int dep[maxn], par[maxn][21], mn[maxn][21];
bool vis[maxn];
struct edge {
int to, l, a;
bool operator < (const edge &other) const { return l > other.l; }
};
vector<edge> G[maxn], G2[maxn];
struct node {
int u, v, l, a;
bool operator < (const node &other) const { return a > other.a; }
} e[maxn];
void dijkstra() {
priority_queue<edge> q;
memset(dist, 0x3f, sizeof(dist));
q.push((edge){1, 0, 0}), dist[1] = 0;
while (!q.empty()) {
edge v = q.top(); q.pop();
for (int i = 0; i < G[v.to].size(); i++) {
if (dist[G[v.to][i].to] > dist[v.to] + G[v.to][i].l) {
dist[G[v.to][i].to] = dist[v.to] + G[v.to][i].l;
q.push((edge){G[v.to][i].to, dist[G[v.to][i].to], 0});
}
}
}
}
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void dfs(int v, int f) {
for (int i = 1; (1 << i) <= c; i++) {
par[v][i] = par[par[v][i - 1]][i - 1];
}
for (int i = 1; (1 << i) <= c; i++) {
mn[v][i] = min(mn[v][i - 1], mn[par[v][i - 1]][i - 1]);
}
for (int i = 0; i < G2[v].size(); i++) {
if (G2[v][i].to == f) continue;
par[G2[v][i].to][0] = v, mn[G2[v][i].to][0] = G2[v][i].a;
dep[G2[v][i].to] = dep[v] + 1;
dfs(G2[v][i].to, v);
dist[v] = min(dist[v], dist[G2[v][i].to]);
}
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &m);
for (int i = 1; i <= (n << 1); i++) G[i].clear(), G2[i].clear();
for (int i = 1, u, v, l, a; i <= m; i++) {
scanf("%d %d %d %d", &u, &v, &l, &a);
G[u].push_back((edge){v, l, a}), G[v].push_back((edge){u, l, a});
e[i] = (node){u, v, l, a};
}
dijkstra(), sort(e + 1, e + m + 1);
for (int i = 1; i <= (n << 1); i++) fa[i] = i;
c = n;
for (int i = 1; i <= m; i++) {
int u = find(e[i].u), v = find(e[i].v);
if (u == v) continue;
G2[++c].push_back((edge){u, 0, e[i].a});
G2[u].push_back((edge){c, 0, e[i].a});
G2[c].push_back((edge){v, 0, e[i].a});
G2[v].push_back((edge){c, 0, e[i].a});
fa[u] = fa[v] = c;
}
memset(par, 0, sizeof(par));
dep[c] = 1, dfs(c, 0);
scanf("%d %d %d", &q, &K, &S);
lastans = 0;
for (int i = 1, v, p; i <= q; i++) {
scanf("%d %d", &v, &p);
v = (v + K * lastans - 1) % n + 1, p = (p + K * lastans) % (S + 1);
for (int j = 20; ~j; j--) {
if (dep[v] - (1 << j) > 0 && mn[v][j] > p) {
v = par[v][j];
}
}
printf("%d\n", lastans = dist[v]);
}
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 8.468 ms | 51 MB + 936 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 8.418 ms | 51 MB + 948 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 8.59 ms | 51 MB + 964 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 8.808 ms | 51 MB + 984 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 14.021 ms | 52 MB + 596 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 1.115 s | 135 MB + 584 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 12.668 ms | 52 MB + 476 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 12.673 ms | 52 MB + 484 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 12.699 ms | 52 MB + 480 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 915.095 ms | 119 MB + 316 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 917.225 ms | 119 MB + 328 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 1.202 s | 141 MB + 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.2 s | 141 MB + 116 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.202 s | 141 MB + 140 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 14.421 ms | 52 MB + 628 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 14.474 ms | 52 MB + 632 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.202 s | 141 MB + 112 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 1.201 s | 141 MB + 116 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.787 s | 144 MB + 592 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.792 s | 144 MB + 596 KB | Accepted | Score: 5 | 显示更多 |