#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define ll long long
#define P pair<ll,ll>
#define mp make_pair
#define fi first
#define se second
#define N 400100
using namespace std;
ll n,m,T,faz[N],tt,d[N],fa[N][20],h[N],Q,K,S,last;
P tmp;
struct Bn
{
ll u,v,a;
bool operator < (const Bn &u) const
{
return a>u.a;
}
}bn[N];
priority_queue<P,vector<P>,greater<P> >pq;
vector<P>to[N];
ll ff(ll u){return u==faz[u]?u:faz[u]=ff(faz[u]);}
inline ll find(ll u,ll v)
{
ll i,t;
for(i=19;i>=0;i--)
{
t=fa[u][i];
if(h[t]>v) u=t;
}
return u;
}
int main()
{
ll i,j,p,q,t;
cin>>T;
while(T--)
{
last=0;
memset(d,0x3f,sizeof(d));
memset(fa,0,sizeof(fa));
scanf("%lld%lld",&n,&m);
tt=n;
for(i=1;i<2*n;i++) faz[i]=i;
for(i=1;i<=m;i++)
{
scanf("%lld%lld%lld%lld",&bn[i].u,&bn[i].v,&t,&bn[i].a);
to[bn[i].u].push_back(mp(bn[i].v,t));
to[bn[i].v].push_back(mp(bn[i].u,t));
}
d[1]=0;
pq.push(mp(0,1));
for(;!pq.empty();)
{
tmp=pq.top();
pq.pop();
p=tmp.fi,q=tmp.se;
if(d[q]<p) continue;
for(i=0;i<to[q].size();i++)
{
t=to[q][i].fi;
if(p+to[q][i].se>=d[t]) continue;
d[t]=p+to[q][i].se;
pq.push(mp(d[t],t));
}
}
// for(i=1;i<=n;i++) cout<<d[i]<<" ";puts("");
sort(bn+1,bn+m+1);
for(i=1;i<=m;i++)
{
p=ff(bn[i].u),q=ff(bn[i].v);
if(p==q) continue;
tt++;
h[tt]=bn[i].a;
d[tt]=min(d[p],d[q]);
fa[p][0]=fa[q][0]=tt;
faz[p]=faz[q]=tt;
// cout<<" "<<tt<<" "<<p<<endl<<" "<<tt<<" "<<q<<endl;
}
for(i=1;(1 << i)<=tt;i++)
{
for(j=1;j<=tt;j++)
{
fa[j][i]=fa[fa[j][i-1]][i-1];
}
}
// for(i=1;i<=tt;i++) cout<<fa[i][1]<<" ";puts("");
scanf("%lld%lld%lld",&Q,&K,&S);
for(i=1;i<=Q;i++)
{
scanf("%lld%lld",&p,&q);
p=(p+K*last-1)%n+1;
q=(q+K*last)%(S+1);
p=find(p,q);
printf("%lld\n",d[p]);
last=d[p];
}
for(i=1;i<=n;i++) to[i].clear();
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 11.535 ms | 73 MB + 308 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 11.561 ms | 73 MB + 312 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 11.77 ms | 73 MB + 328 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 11.908 ms | 73 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 16.401 ms | 73 MB + 692 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 962.55 ms | 113 MB + 448 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 15.085 ms | 73 MB + 504 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 15.081 ms | 73 MB + 508 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 15.074 ms | 73 MB + 504 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 766.224 ms | 95 MB + 196 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 766.728 ms | 95 MB + 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 1.103 s | 115 MB + 44 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.103 s | 114 MB + 904 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.103 s | 114 MB + 304 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 16.859 ms | 73 MB + 696 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 16.88 ms | 73 MB + 700 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.104 s | 114 MB + 580 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 1.103 s | 114 MB + 876 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.733 s | 117 MB + 996 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.738 s | 117 MB + 508 KB | Accepted | Score: 5 | 显示更多 |