#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define _(d) while(d((ch=getchar()-48)>=0))
inline int get(){
char ch;_(!);int x=ch;
_() x=(x<<3)+(x<<1)+ch;
return x;
}
const int N=200001,M=800001,inf=0x7f7f7f7f;
int n,m,Q,tt,h[N],to[M],nx[M],c[M],d[N],in[N],p[N],t[N],r[N],v[N];
vector<pair<int,int> >vt[N];
struct info{
int u,v,l,a;
void init(){
u=get(),v=get(),l=get(),a=get();
to[++tt]=v,nx[tt]=h[u],h[u]=tt,c[tt]=l;
to[++tt]=u,nx[tt]=h[v],h[v]=tt,c[tt]=l;
}
bool operator<(const info &r)const{
return a>r.a;
}
}e[M];
struct ele{
int d,p;
ele(int d=0,int p=0):d(d),p(p){}
bool operator<(const ele &r)const{
return d>r.d;
}
};priority_queue<ele>q;
void dijkstra(){
memset(d+1,0x7f,n<<2);
q.push(ele(d[1]=0,1));
while(!q.empty()){
int x=q.top().p;q.pop();
for(int i=h[x];i;i=nx[i])
if(d[x]+c[i]<d[to[i]])
q.push(ele(d[to[i]]=d[x]+c[i],to[i]));
while(!q.empty()&&d[q.top().p]!=q.top().d) q.pop();
}
}
int fnd(int x){
while(x!=p[x]) x=p[x];
return x;
}
void uni(int a,int b,int u){
if(r[a]<r[b]) swap(a,b);
if(r[a]==r[b]) ++r[a];
p[b]=a,t[b]=u,v[a]=min(v[a],v[b]);
vt[a].push_back(make_pair(u,v[b]));
}
int qry(int x,int s){
int u;
for(u=x;u!=p[u];u=p[u])
if(t[u]<=s) break;
return min(d[u],upper_bound(vt[u].begin(),vt[u].end(),make_pair(s,inf))->second);
}
int main(){
for(int T=get();T--;){
n=get(),m=get(),tt=0;
for(int i=1;i<=n;++i) h[i]=r[i]=t[i]=0,p[i]=i;
for(int i=1;i<=m;++i) e[i].init();
dijkstra();
memcpy(v+1,d+1,n<<2);
for(int i=1;i<=n;++i)
vt[i].clear(),vt[i].push_back(make_pair(inf,inf));
sort(e+1,e+m+1);
for(int i=1;i<=m;++i)
uni(fnd(e[i].u),fnd(e[i].v),e[i].a);
for(int i=1;i<=n;++i){
sort(vt[i].begin(),vt[i].end());
for(int j=vt[i].size()-2;j>=0;--j)
vt[i][j].second=min(vt[i][j].second,vt[i][j+1].second);
}
int Q=get(),K=get(),S=get()+1,lan=0;
for(int i=1,v,p;i<=Q;++i){
v=(get()+lan*K-1)%n+1;
p=(get()+lan*K)%S;
printf("%d\n",lan=qry(v,p));
}
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 644.37 us | 4 MB + 640 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 724.39 us | 4 MB + 656 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 780.95 us | 4 MB + 664 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 897.2 us | 4 MB + 676 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 3.777 ms | 5 MB + 16 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 505.044 ms | 44 MB + 920 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 3.212 ms | 4 MB + 888 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 3.276 ms | 4 MB + 888 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 3.209 ms | 4 MB + 884 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 305.52 ms | 30 MB + 556 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 306.671 ms | 30 MB + 572 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 555.939 ms | 45 MB + 300 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 553.983 ms | 45 MB + 132 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 555.87 ms | 45 MB + 348 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 4.545 ms | 5 MB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 4.507 ms | 5 MB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 555.075 ms | 45 MB + 140 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 556.928 ms | 45 MB + 1012 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 910.74 ms | 49 MB + 568 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 908.097 ms | 48 MB + 580 KB | Accepted | Score: 5 | 显示更多 |