rt,同学也帮忙调了,还是没有调出来(5 分是第一个点,其他全部 WA 了)。
#include<bits/stdc++.h>
using namespace std;
const int N = 400005, M = 800005;
int t, head[N], u[M], v[M], dis[N<<1], n, m, h[M], fa[N], hp[N], f[N][35], dep[N], cnt, dp[N];
bool fl[N];
struct stu {
int w, id;
bool operator <(const stu x) const{
return w > x.w;
}
};
priority_queue<stu>q;
stu mak(int w, int id) {
stu x;
x.w = w, x.id = id;
return x;
}
struct node {
int next, to, w, h, from;
} a[M];
void add(int x, int y, int w, int h) {
a[++t].to = y;
a[t].from = x;
a[t].next = head[x];
a[t].w = w;
a[t].h = h;
head[x] = t;
}
void dijkstra() {
memset(dis, 127, sizeof(dis));
dis[1] = 0;
q.push(mak(0, 1));
while (!q.empty()) {
int x = q.top().id;
q.pop();
if (fl[x] == 1)
continue;
fl[x] = 1;
for (int i = head[x]; i != 0; i = a[i].next) {
int y = a[i].to;
if (dis[y] > dis[x] + a[i].w) {
dis[y] = dis[x] + a[i].w;
q.push(mak(dis[y], y));
}
}
}
return ;
}
bool cmp(node a, node b) {
return a.h > b.h;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void kruskal() {
sort(a + 1, a + t + 1, cmp);
for (int i = 1; i <= t; ++i)
u[i] = a[i].from, v[i] = a[i].to, h[i] = a[i].h;
int tt = t;
t = 0;
memset(head, 0, sizeof(head));
cnt = n;
for (int i = 1; i <= n; ++i)
fa[i] = i;
for (int i = 1; i <= tt; ++i) {
if (find(u[i]) == find(v[i]))
continue;
++cnt;
add(cnt, find(u[i]), 0, 0);
add(cnt, find(v[i]), 0, 0);
// add(find(u[i]), cnt, 0, 0);
// add(find(v[i]), cnt, 0, 0);
fa[find(u[i])] = fa[find(v[i])] = fa[cnt] = cnt;
hp[cnt] = h[i];
}
return ;
}
void dfs(int rt, int fat) {
f[rt][0] = fat;
for (int i = 1; i <= 30; ++i)
f[rt][i] = f[f[rt][i-1]][i-1];
for (int i = head[rt]; i != 0; i = a[i].next)
if (a[i].to != fat)
dfs(a[i].to, rt);
return ;
}
void pre(int rt, int fat) {
dp[rt] = dis[rt];
for(int i = head[rt]; i != 0; i = a[i].next)
if (a[i].to != fat)
pre(a[i].to, fat), dp[rt] = min(dp[rt], dp[a[i].to]);
return ;
}
int query(int hi, int x) {
for (int i = 30; i >= 0; --i)
if(hp[f[x][i]] > hi)
x = f[x][i];
return dp[x];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
// freopen("return3.in","r",stdin);
int T;
cin >> T;
while (T--) {
memset(head, 0, sizeof(head));
memset(f, 0, sizeof(f));
t = 0;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y, w, z;
cin >> x >> y >> w >> z;
add(x, y, w, z);
add(y, x, w, z);
}
dijkstra();
kruskal();
dfs(cnt, 0);
pre(cnt, 0);
int q, k, s, las = 0;
cin >> q >> k >> s;
for (int i = 1; i <= q; ++i) {
int x, y;
cin >> x >> y;
x = (x + k * las - 1) % n + 1;
y = (y + k * las) % (s + 1);
las = query(y, x);
cout << las << '\n';
}
}
return 0;
}
谢谢!