//sro zbw ak noi2018 orz
#include<bits/stdc++.h>
using namespace std;
#define FE "return"
#define pb push_back
#define clr(a) memset(a,0,sizeof(a))
const int N=500005,N2=N<<1,inf=0x3f3f3f3f;
int n,m,d[N2],mn[N2],vis[N],t,fa[N2][20],uf[N2],lust,Q,k,s;
struct node{int d,n;};
bool operator<(node a,node b){return b.d<a.d;}
priority_queue<node>q;
vector<int>e[N],f[N];
void dbg(string a,node x){cerr<<a<<" ("<<x.d<<","<<x.n<<")\n";}
struct edge{int u,v,w,x;}ed[N2];
bool cmp(edge a,edge b){return b.x<a.x;}
void init();
int find(int a){if(uf[a])return uf[a]=find(uf[a]);return a;}
void build(){clr(fa),clr(uf),clr(mn);
for(int i=1;i<=n;i++)mn[i]=inf;
for(int i=0,j=n;i<m;i++){
int v=find(ed[i].v),u=find(ed[i].u);
if(v==u)continue;
uf[v]=uf[u]=++j,fa[v][0]=fa[u][0]=j,d[j]=inf,mn[j]=ed[i].x;
}n=(n<<1)-1;
for(int i=1;i<=n;i++)
d[fa[i][0]]=min(d[fa[i][0]],d[i]),mn[fa[i][0]]=min(mn[fa[i][0]],mn[i]);
for(int k=1;k<20;k++)for(int i=1;i<=n;i++)fa[i][k]=fa[fa[i][k-1]][k-1];
//for(int i=1;i<=n;i++)printf("%d %d %d\n",d[i],mn[i],fa[i][0]);
}
void solve(){lust=0;
scanf("%d%d%d",&Q,&k,&s);
while(Q--){
int a,b;scanf("%d%d",&a,&b);
a=(a+(long long)k*lust-1)%((n+1)>>1)+1,b=(b+(long long)k*lust)%(s+1);//printf("query %d %d\n",a,b);
for(int k=19;~k;k--)if(b<mn[fa[a][k]])a=fa[a][k];
printf("%d\n",lust=d[a]);
}
}
int main(){
freopen(FE".in","r",stdin);
freopen(FE".out","w",stdout);
scanf("%d",&t);while(t--){
init();
build();
solve();
}return 0;
}
void init(){clr(vis),clr(d);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)e[i].clear(),f[i].clear();
for(int i=0;i<m;i++){
int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);
e[a].pb(b),f[a].pb(c);
e[b].pb(a),f[b].pb(c);
ed[i]=(edge){a,b,c,d};
}sort(ed,ed+m,cmp);
for(int i=2;i<=n;i++)d[i]=inf;
q.push((node){0,1});
while(!q.empty()){
node nod=q.top();q.pop();//dbg("out",nod);
int v=nod.n;
if(vis[v])continue;vis[v]=1;//dbg("ok",nod);
for(int i=0;i<(int)e[v].size();i++){
int u=e[v][i],w=d[v]+f[v][i];//dbg("chk",(node){w,u});
if(!vis[u]&&w<d[u])d[u]=w,q.push((node){w,u});//dbg("in",(node){w,u});
}
}//for(int i=1;i<=n;i++){if(d[i]==inf)d[i]=2147483647;printf("%d ",d[i]);}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 17.83 ms | 112 MB + 584 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 17.962 ms | 112 MB + 588 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 18.023 ms | 112 MB + 596 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 18.315 ms | 112 MB + 604 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 22.654 ms | 112 MB + 828 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 1.056 s | 137 MB + 932 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 21.579 ms | 112 MB + 744 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 21.457 ms | 112 MB + 748 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 21.467 ms | 112 MB + 744 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 823.573 ms | 130 MB + 376 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 824.123 ms | 130 MB + 380 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 1.203 s | 139 MB + 520 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.204 s | 139 MB + 364 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.204 s | 138 MB + 800 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 23.228 ms | 112 MB + 832 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 23.33 ms | 112 MB + 836 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.203 s | 139 MB + 32 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 1.203 s | 139 MB + 316 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.767 s | 142 MB + 444 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.77 s | 141 MB + 964 KB | Accepted | Score: 5 | 显示更多 |