#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
typedef long long LL;
const int N = 600005, INF = 2e9 + 7, LOG = 21;
int tc, n, m, Qi, k, s;
int dis[N], flg[N], val[N], mdi[N], gr[LOG][N];
std::priority_queue<std::pair<int, int> > Q;
inline void Read(int &x) {
x = 0; static char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar());
for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
}
struct Edge {
int u, v, a;
inline friend bool operator < (Edge a, Edge b) {
return a.a > b.a;
}
} e[N];
int yun, las[N], to[N << 1], pre[N << 1], wi[N << 1];
inline void Add(int a, int b, int c = 0) {
to[++yun] = b; wi[yun] = c; pre[yun] = las[a]; las[a] = yun;
}
void Gragh_clear() {
memset(gr, 0, sizeof gr);
memset(las, 0, sizeof las);
yun = 0;
}
namespace DSU {
int fa[N];
void Init() {
for (int i = 1; i <= n + m; ++i) {
fa[i] = i;
if (i <= n) mdi[i] = dis[i], val[i] = -1;
}
}
int Seek(int x) {
return (x == fa[x])? (x) : (fa[x] = Seek(fa[x]));
}
void Merge(int x, int y) {
fa[Seek(y)] = x;
}
}
void Dij() {
for (int i = 1; i <= n; ++i) {
dis[i] = INF; flg[i] = 0;
}
dis[1] = 0;
Q.push(std::make_pair(0, 1));
for (; !Q.empty(); ) {
int x = Q.top().second; Q.pop();
if (flg[x]) continue;
flg[x] = 1;
for (int i = las[x]; i; i = pre[i]) {
if (dis[to[i]] > dis[x] + wi[i]) {
dis[to[i]] = dis[x] + wi[i];
Q.push(std::make_pair(-dis[to[i]], to[i]));
}
}
}
}
int main() {
scanf("%d", &tc);
for (; tc; --tc) {
scanf("%d%d", &n, &m);
Gragh_clear();
for (int i = 1, x, y, a, l; i <= m; ++i) {
//scanf("%d%d%d%d", &x, &y, &l, &a);
Read(x); Read(y); Read(l); Read(a);
Add(x, y, l); Add(y, x, l);
e[i] = (Edge) { x, y, a };
}
Dij(); DSU::Init();
std::sort(e + 1, e + 1 + m);
for (int i = 1; i <= m; ++i) {
int x = DSU::Seek(e[i].u), y = DSU::Seek(e[i].v);
if (x == y) continue;
val[i + n] = e[i].a;
mdi[i + n] = std::min(mdi[x], mdi[y]);
DSU::Merge(i + n, x); DSU::Merge(i + n, y);
gr[0][x] = gr[0][y] = i + n;
}
for (int i = 1; i < LOG; ++i) {
for (int j = 1; j <= n + m; ++j) {
if (gr[i - 1][j]) gr[i][j] = gr[i - 1][gr[i - 1][j]];
}
}
scanf("%d%d%d", &Qi, &k, &s);
for (int x, a, lans = 0; Qi; --Qi) {
//scanf("%d%d", &x, &a);
Read(x); Read(a);
x = (x + (LL) k * lans - 1) % n + 1;
a = (a + (LL) k * lans) % (s + 1);
for (int i = LOG - 1; ~i; --i) {
if (gr[i][x] && val[gr[i][x]] > a) x = gr[i][x];
}
printf("%d\n", mdi[x]);
lans = mdi[x];
}
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 7.796 ms | 50 MB + 408 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 7.839 ms | 50 MB + 424 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 7.957 ms | 50 MB + 436 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 8.1 ms | 50 MB + 436 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 11.314 ms | 50 MB + 660 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 710.585 ms | 74 MB + 52 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 10.443 ms | 50 MB + 556 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 10.502 ms | 50 MB + 560 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 10.44 ms | 50 MB + 556 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 531.006 ms | 65 MB + 932 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 532.4 ms | 65 MB + 936 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 740.52 ms | 74 MB + 868 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 740.009 ms | 74 MB + 780 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 739.431 ms | 74 MB + 212 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 11.851 ms | 50 MB + 664 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 11.839 ms | 50 MB + 668 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 740.766 ms | 74 MB + 428 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 739.723 ms | 74 MB + 720 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.489 s | 77 MB + 872 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.489 s | 77 MB + 364 KB | Accepted | Score: 5 | 显示更多 |