#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define fo(i,j,k) for(int i=j;i<=k;++i)
#define fd(i,j,k) for(int i=j;i>=k;--i)
#define rep(i,x) for(int i=ls[x];i;i=nx[i])
using namespace std;
const int N=2e5+10,M=8e5+10,inf=1e9;
typedef pair<int,int> par;
priority_queue<par,vector<par>,greater<par> >q;
int to[M],nx[M],ls[N<<1],vl[M],num=0;
void link(int u,int v,int w){
to[++num]=v,nx[num]=ls[u],ls[u]=num;
vl[num]=w;
}
void clear(){
memset(ls,0,sizeof(ls)),num=0;
}
int read(){
char ch=' ';int t=0;
for(;ch<'0' || ch>'9';ch=getchar());
for(;ch>='0' && ch<='9';ch=getchar()) t=(t<<1)+(t<<3)+ch-48;
return t;
}
struct node{
int u,v,l,a;
}e[M];
bool cmp(node x,node y) {return x.a<y.a;}
int d[N];
bool vis[N];
void dijstra(){
memset(vis,0,sizeof(vis));
memset(d,60,sizeof(d)),d[1]=0;
q.push(make_pair(d[1],1));
for(;;){
int x=q.top().second;
q.pop();
vis[x]=1;
rep(i,x){
int v=to[i];
if(vis[v]) continue;
if(d[x]+vl[i]<d[v]){
d[v]=d[x]+vl[i];
q.push(make_pair(d[v],v));
}
}
if(q.empty()) break;
}
}
int f[N<<1],tot=0;
int find(int x){
return !f[x]?x:f[x]=find(f[x]);
}
int n,m;
int fa[N<<1][20],g[N<<1][20];
int w[N<<1];
void pre(int x){
fo(i,1,19){
fa[x][i]=fa[fa[x][i-1]][i-1];
g[x][i]=min(g[x][i-1],g[fa[x][i-1]][i-1]);
}
w[x]=x>n?inf:d[x];
rep(i,x){
int v=to[i];
fa[v][0]=x,g[v][0]=vl[i];
pre(v),w[x]=min(w[x],w[v]);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
fo(i,1,m){
node x;
x.u=read(),x.v=read(),x.l=read(),x.a=read();
link(x.u,x.v,x.l),link(x.v,x.u,x.l);
e[i]=x;
}
dijstra();
sort(e+1,e+m+1,cmp);
clear();
memset(f,0,sizeof(f)),tot=n;
fd(i,m,1){
int u=find(e[i].u),v=find(e[i].v);
if(u!=v){
tot++,f[u]=f[v]=tot;
link(tot,u,e[i].a),link(tot,v,e[i].a);
}
}
pre(tot);
int Q=read(),K=read(),S=read();
int ans=0;
while(Q--){
int x=(read()+K*ans-1)%n+1,p=(read()+K*ans)%(S+1);
fd(i,19,0)
if(fa[x][i] && g[x][i]>p) x=fa[x][i];
printf("%d\n",ans=w[x]);
}
clear();
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 611.22 us | 4 MB + 56 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 634.49 us | 4 MB + 72 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 753.63 us | 4 MB + 84 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 884.52 us | 4 MB + 112 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 4.406 ms | 4 MB + 756 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 739.769 ms | 84 MB + 912 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 3.677 ms | 4 MB + 684 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 3.65 ms | 4 MB + 688 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 3.645 ms | 4 MB + 688 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 533.363 ms | 79 MB + 88 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 535.355 ms | 79 MB + 96 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 840.853 ms | 90 MB + 636 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 840.701 ms | 90 MB + 616 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 840.913 ms | 90 MB + 644 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 5.13 ms | 4 MB + 788 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 5.124 ms | 4 MB + 792 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 840.649 ms | 90 MB + 628 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 839.027 ms | 90 MB + 608 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.361 s | 94 MB + 72 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.365 s | 94 MB + 96 KB | Accepted | Score: 5 | 显示更多 |