#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include<cstdio>
#include<cctype>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
const int maxn=800005;
struct Edge {
int u,v,l,a;
Edge(int u=0,int v=0,int l=0,int a=0):u(u),v(v),l(l),a(a){}
inline bool operator < (const Edge &rhs) const {
return a>rhs.a;
}
}E[maxn];
vector<Edge>edges;
vector<int>G[maxn];
int n,m,T,fa[maxn],anc[maxn],val[maxn],dis[maxn],hei[maxn],g[maxn][20],q,k,s;
bool vis[maxn];
struct Node {
int u;
ll d;
Node(int u=0,ll d=0):u(u),d(d){}
inline bool operator < (const Node &rhs) const {
return d>rhs.d;
}
};
priority_queue<Node>Q;
inline void Dijkstra() {
while(!Q.empty())
Q.pop();
memset(dis,0x3f,sizeof dis);
memset(vis,0,sizeof vis);
dis[1]=0;
Q.push(Node(1,0));
while(!Q.empty()) {
Node now=Q.top();
Q.pop();
int u=now.u;
ll d=now.d;
if(vis[u])
continue;
vis[u]=1;
for(int i=0;i<(int)G[u].size();++i) {
Edge &e=edges[G[u][i]];
int v=e.v;
if(dis[v]>d+e.l) {
dis[v]=d+e.l;
Q.push(Node(v,dis[v]));
}
}
}
}
inline int gf(int x) {
return x==fa[x]?x:fa[x]=gf(fa[x]);
}
int main() {
read(T);
while(T--) {
read(n),read(m);
edges.clear();
for(int i=1;i<=n;++i)
G[i].clear();
for(int i=1;i<=m;++i)
read(E[i].u),read(E[i].v),read(E[i].l),read(E[i].a);
read(q),read(k),read(s);
sort(E+1,E+m+1);
for(int i=1;i<=m;++i) {
edges.push_back(Edge(E[i].u,E[i].v,E[i].l));
G[E[i].u].push_back(edges.size()-1);
edges.push_back(Edge(E[i].v,E[i].u,E[i].l));
G[E[i].v].push_back(edges.size()-1);
}
Dijkstra();
for(int i=1;i<=n;++i)
fa[i]=anc[i]=i;
int cnt=n;
for(int i=1;i<=m;++i) {
int u=E[i].u,v=E[i].v;
cnt++,fa[cnt]=cnt,fa[u]=gf(u),fa[v]=gf(v);
dis[cnt]=min(dis[fa[u]],dis[fa[v]]);
anc[cnt]=anc[fa[u]]=anc[fa[v]]=cnt;
fa[fa[u]]=fa[fa[v]]=cnt;
hei[cnt]=E[i].a;
}
for(int i=1;i<=cnt;++i)
g[i][0]=anc[i];
for(int i=1;i<20;++i)
for(int j=1;j<=cnt;++j)
g[j][i]=g[g[j][i-1]][i-1];
for(int i=1;i<=n;++i)
hei[i]=s+1;
int lstans=0;
while(q--) {
int x,y;
read(x),read(y);
x=(x+k*lstans-1)%n+1;
y=(y+k*lstans)%(s+1);
for(int i=19;~i;--i)
if(hei[g[x][i]]>y)
x=g[x][i];
printf("%d\n",lstans=dis[x]);
}
}
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 6.639 ms | 34 MB + 384 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 6.567 ms | 34 MB + 392 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 6.696 ms | 34 MB + 408 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 6.893 ms | 34 MB + 452 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 10.735 ms | 35 MB + 136 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 920.474 ms | 117 MB + 424 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 9.547 ms | 34 MB + 812 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 9.457 ms | 34 MB + 816 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 9.559 ms | 34 MB + 812 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 580.22 ms | 86 MB + 132 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 581.192 ms | 86 MB + 132 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 1.129 s | 118 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.129 s | 118 MB + 176 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.128 s | 117 MB + 604 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 11.405 ms | 35 MB + 132 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 11.455 ms | 35 MB + 136 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.129 s | 117 MB + 876 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 1.128 s | 118 MB + 140 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.995 s | 121 MB + 272 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.994 s | 120 MB + 796 KB | Accepted | Score: 5 | 显示更多 |