#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
inline bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
inline bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;}
inline int read() {
int x = 0, fh = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * fh;
}
void File() {
freopen ("return.in", "r", stdin);
freopen ("return.out", "w", stdout);
}
int n, m;
const int N = 4e5 + 1e3, M = 8e5 + 1e3;
struct Edge {
int u, v, a;
inline bool operator < (const Edge &rhs) const {
return a > rhs.a;
}
} lt[N];
typedef pair<int, int> PII;
#define fir first
#define sec second
#define mp make_pair
namespace Dijsktra {
int Head[N], Next[M], to[M], val[M], e = 0;
void Init() {
Set(Head, 0);
e = 0;
}
inline void add_edge(int u, int v, int w) { to[++ e] = v; Next[e] = Head[u]; val[e] = w; Head[u] = e; }
priority_queue<PII, vector<PII>, greater<PII> > P;
bool vis[N]; int dis[N];
void Run() {
Set(vis, false);
Set(dis, 0x7f); dis[1] = 0; P.push(mp(0, 1));
while (!P.empty()) {
PII cur = P.top(); int u = cur.sec; P.pop();
if (vis[u]) continue ;
vis[u] = true;
for (int i = Head[u]; i; i = Next[i]) {
int v = to[i];
if (chkmin(dis[v], dis[u] + val[i]))
P.push(mp(dis[v], v));
}
}
}
}
namespace Kruskal {
int mina[20][N], mind[N], to[20][N], fa[N], Logn, num = 0;
void Init(int *bas) {
For (i, 1, n)
fa[i] = i, mind[i] = bas[i]; num = n;
}
int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
inline int Min(int x, int y) {
return x < y ? x : y;
}
void Build() {
sort(lt + 1, lt + 1 + m);
For (i, 1, m) {
int x = lt[i].u, y = lt[i].v, alt = lt[i].a;
int rtx = find(x), rty = find(y);
if (rtx == rty) continue ;
mind[++ num] = min(mind[rtx], mind[rty]);
fa[num] =
to[0][rtx] = fa[rtx] =
to[0][rty] = fa[rty] = num;
mina[0][rtx] = mina[0][rty] = alt;
}
Logn = ceil(log(num) / log(2));
For (j, 1, Logn) For (i, 1, num) {
to[j][i] = to[j - 1][to[j - 1][i]];
mina[j][i] = Min(mina[j - 1][i], mina[j - 1][to[j - 1][i]]);
}
}
inline int Query(int pos, int lim) {
Fordown (i, Logn, 0)
if (mina[i][pos] > lim) pos = to[i][pos];
return mind[pos];
}
}
int q, k, s;
int main () {
int cases = read();
while (cases --) {
n = read(); m = read();
Dijsktra :: Init();
For (i, 1, m) {
int u = read(), v = read(), l = read(), a = read();
lt[i] = (Edge) {u, v, a};
Dijsktra :: add_edge(u, v, l);
Dijsktra :: add_edge(v, u, l);
}
Dijsktra :: Run();
Kruskal :: Init(Dijsktra :: dis); Kruskal :: Build();
q = read(); k = read(); s = read();
int ans = 0;
while (q --) {
int v = (1ll * read() + k * ans - 1) % n + 1;
int p = (1ll * read() + k * ans) % (s + 1);
printf ("%d\n", ans = Kruskal :: Query(v, p));
}
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 466.25 us | 3 MB + 504 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 493.3 us | 3 MB + 556 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 633.71 us | 3 MB + 600 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 727.51 us | 3 MB + 616 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 3.627 ms | 4 MB + 104 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 524.42 ms | 83 MB + 160 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 3.071 ms | 4 MB + 20 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 3.071 ms | 4 MB + 24 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 3.072 ms | 4 MB + 20 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 515.317 ms | 77 MB + 144 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 518.901 ms | 77 MB + 148 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 694.543 ms | 84 MB + 776 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 694.08 ms | 84 MB + 612 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 693.803 ms | 84 MB + 20 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 4.312 ms | 4 MB + 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 4.31 ms | 4 MB + 112 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 694.644 ms | 84 MB + 288 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 693.391 ms | 84 MB + 576 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.326 s | 87 MB + 708 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.327 s | 87 MB + 208 KB | Accepted | Score: 5 | 显示更多 |