#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,T,q,x,y,z,zz,k,s,v0,p0,lastans,v,p,nowans;
const int N=1000000,INF=1e9;
int dis[N],vis[N],f[N],fa[N],ans[N],out[N],FA[N][20],H[N];
struct link
{
int top,fi[N],ne[N],la[N],to[N],l[N],h[N];
inline void clear()
{
top=0,memset(fi,0,sizeof fi),memset(ne,0,sizeof ne),memset(la,0,sizeof la),memset(to,0,sizeof to),memset(l,0,sizeof l),memset(h,0,sizeof h);
}
inline void add(int x,int y,int z,int H)
{
top++,to[top]=y,l[top]=z,h[top]=H;
if(fi[x]==0)fi[x]=top;else ne[la[x]]=top;
la[x]=top;
}
}L;
struct node
{
int id,dis;
bool operator < (const node &a) const {return a.dis < dis;}
};
priority_queue<node> que;
inline void DIJKSTRA()
{
for(int i=1;i<=n;i++)dis[i]=INF,vis[i]=0;
dis[1]=0;while(!que.empty())que.pop();
node tmp;
tmp.id=1;tmp.dis=0;que.push(tmp);
while (!que.empty())
{
int now=que.top().id;
que.pop();
if (vis[now]) continue;
vis[now]=true;
for (int i=L.fi[now];i;i=L.ne[i])
{
if (!vis[L.to[i]]&&dis[L.to[i]]>dis[now]+L.l[i])
{
dis[L.to[i]]=dis[now]+L.l[i];
tmp.id=L.to[i];tmp.dis=dis[L.to[i]];
que.push(tmp);
}
}
}
}
struct E{int x,y,l,h;}e[N];
inline bool cmp(E a,E b){return a.h>b.h;}
inline int ASK(int x){if(fa[x]==x)return x;else return fa[x]=ASK(fa[x]);}
inline int Read()
{
int x=0,f=1;char c=getchar();
while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
int main()
{
T=Read();
while(T--)
{
L.clear();lastans=0;n=Read(),m=Read();
for(int i=1;i<=2*n;i++)fa[i]=i;
for(int i=1;i<=m;i++)
{
x=Read(),y=Read(),z=Read(),zz=Read();
e[i].x=x,e[i].y=y,e[i].l=z,e[i].h=zz;
L.add(x,y,z,zz),L.add(y,x,z,zz);
}
DIJKSTRA();sort(e+1,e+1+m,cmp);int tot=n;
for(int i=1;i<=n;i++)ans[i]=dis[i],H[i]=1e9;
q=Read(),k=Read(),s=Read();
for(int i=1;i<=m;i++)
{
int fx=ASK(e[i].x),fy=ASK(e[i].y);
if(fx!=fy)
{
tot++;fa[tot]=tot;fa[fx]=fa[fy]=tot;ans[tot]=min(ans[fx],ans[fy]);FA[fx][0]=FA[fy][0]=tot;
H[tot]=e[i].h;
}
}
while(q--)
{
scanf("%d%d",&v0,&p0);
v=(v0+k*lastans-1)%n+1;
p=(p0+k*lastans)%(s+1);
int now=v;lastans=1e9;
while(H[now]>p)lastans=min(lastans,ans[now]),now=FA[now][0];
printf("%d\n",lastans);
}
}
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 4.357 ms | 22 MB + 952 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 4.333 ms | 22 MB + 960 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 4.464 ms | 22 MB + 972 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 4.663 ms | 22 MB + 996 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 7.87 ms | 23 MB + 308 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 2.248 s | 67 MB + 352 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 11.26 ms | 23 MB + 276 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 11.305 ms | 23 MB + 280 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 11.157 ms | 23 MB + 276 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 4 s | 62 MB + 804 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #11 | 4 s | 62 MB + 804 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #12 | 4 s | 55 MB + 32 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #13 | 4 s | 54 MB + 712 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #14 | 4 s | 55 MB + 28 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #15 | 15.264 ms | 23 MB + 312 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #16 | 15.345 ms | 23 MB + 316 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #17 | 4 s | 54 MB + 484 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #18 | 4 s | 54 MB + 672 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #19 | 4 s | 54 MB + 708 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #20 | 4 s | 54 MB + 704 KB | Time Limit Exceeded | Score: 0 | 显示更多 |