#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(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);
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 | 304.94 us | 2 MB + 344 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 326.04 us | 2 MB + 376 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 457.1 us | 2 MB + 400 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 586.84 us | 2 MB + 404 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 4.043 ms | 2 MB + 892 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 793.983 ms | 69 MB + 68 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 2.976 ms | 2 MB + 640 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 2.985 ms | 2 MB + 648 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 2.987 ms | 2 MB + 644 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 531.195 ms | 41 MB + 528 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #11 | 556.859 ms | 41 MB + 512 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #12 | 987.634 ms | 71 MB + 284 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 987.51 ms | 71 MB + 120 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 984.707 ms | 70 MB + 552 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 4.788 ms | 2 MB + 896 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #16 | 4.781 ms | 2 MB + 900 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #17 | 988.18 ms | 70 MB + 820 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #18 | 986.153 ms | 71 MB + 84 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #19 | 2.316 s | 74 MB + 216 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #20 | 2.306 s | 73 MB + 740 KB | Accepted | Score: 5 | 显示更多 |