#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
const int maxn=200000;
const int maxm=400000;
const int inf=0x3f3f3f3f;
int read()
{
int x=0,f=1;
char ch=getchar();
while((ch<'0')||(ch>'9'))
{
if(ch=='-')
{
f=-f;
}
ch=getchar();
}
while((ch>='0')&&(ch<='9'))
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
namespace dsu
{
int fa[maxn*2+10];
int clear()
{
memset(fa,0,sizeof fa);
return 0;
}
int find(int x)
{
return fa[x]?fa[x]=find(fa[x]):x;
}
}
struct edge
{
int u,v,l;
edge(int u_=0,int v_=0,int l_=0)
{
u=u_;
v=v_;
l=l_;
}
bool operator <(const edge &other) const
{
return l<other.l;
}
};
int n,m;
namespace tree
{
int pre[maxn*2+10],now[maxn*2+10],son[maxn*2+10],tot;
int val[maxn+10],fa[maxn*2+10][20],dist[maxn*2+10];
int clear()
{
tot=0;
memset(now,0,sizeof now);
memset(val,0,sizeof val);
return 0;
}
int ins(int a,int b)
{
pre[++tot]=now[a];
now[a]=tot;
son[tot]=b;
return 0;
}
int getval()
{
for(int i=1; i<=18; ++i)
{
for(int j=1; j<=2*n+1; ++j)
{
fa[j][i]=fa[fa[j][i-1]][i-1];
}
}
return 0;
}
int dfs(int u,int f)
{
fa[u][0]=f;
if(u>=n)
{
return 0;
}
int j=now[u];
dist[u]=inf;
while(j)
{
int v=son[j];
dfs(v,u);
dist[u]=std::min(dist[u],dist[v]);
j=pre[j];
}
return 0;
}
int getdist(int v,int p)
{
v=v+n-1;
if(val[fa[v][0]]<=p)
{
return dist[v];
}
v=fa[v][0];
for(int i=18; i>=0; --i)
{
if(fa[v][i]&&(val[fa[v][i]]>p))
{
v=fa[v][i];
}
}
return dist[v];
}
}
struct data
{
int dist,id;
data(int dist_=0,int id_=0)
{
dist=dist_;
id=id_;
}
bool operator <(const data &other) const
{
return dist>other.dist;
}
};
namespace graph
{
int pre[maxm*2+10],now[maxn+10],son[maxm*2+10],tot;
int dist[maxn+10],val[maxm*2+10],cnt,vis[maxn+10];
edge e[maxm+10];
std::priority_queue<data> h;
int clear()
{
tot=cnt=0;
memset(now,0,sizeof now);
return 0;
}
int ins(int a,int b,int c)
{
pre[++tot]=now[a];
now[a]=tot;
son[tot]=b;
val[tot]=c;
return 0;
}
int add(int a,int b,int c,int d)
{
ins(a,b,c);
ins(b,a,c);
e[++cnt]=edge(a,b,d);
return 0;
}
int kruskal()
{
std::sort(e+1,e+cnt+1);
int need=n-1;
for(int i=cnt; i; --i)
{
int fu=dsu::find(e[i].u+n-1),fv=dsu::find(e[i].v+n-1);
if(fu!=fv)
{
dsu::fa[fu]=dsu::fa[fv]=need;
tree::ins(need,fu);
tree::ins(need,fv);
tree::val[need]=e[i].l;
--need;
if(!need)
{
break;
}
}
}
return 0;
}
int dijkstra(int s)
{
memset(dist,63,sizeof dist);
dist[s]=0;
memset(vis,0,sizeof vis);
h.push(data(0,s));
while(!h.empty())
{
data w=h.top();
h.pop();
if(vis[w.id])
{
continue;
}
vis[w.id]=1;
int j=now[w.id];
while(j)
{
int v=son[j];
if(dist[w.id]+val[j]<dist[v])
{
dist[v]=dist[w.id]+val[j];
h.push(data(dist[v],v));
}
j=pre[j];
}
}
for(int i=1; i<=n; ++i)
{
tree::dist[i+n-1]=dist[i];
}
return 0;
}
}
int main()
{
int t=read();
while(t--)
{
dsu::clear();
tree::clear();
graph::clear();
n=read();
m=read();
while(m--)
{
int u=read(),v=read(),l=read(),h=read();
graph::add(u,v,l,h);
}
graph::kruskal();
graph::dijkstra(1);
tree::dfs(1,0);
tree::getval();
int q=read(),k=read(),s=read();
int lastans=0;
while(q--)
{
int v=read(),p=read();
v=(v+lastans*k-1)%n+1;
p=(p+lastans*k)%(s+1);
lastans=tree::getdist(v,p);
printf("%d\n",lastans);
}
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 1.494 ms | 10 MB + 744 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 1.515 ms | 10 MB + 748 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 1.622 ms | 10 MB + 764 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 1.727 ms | 10 MB + 780 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 4.614 ms | 11 MB + 136 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 599.988 ms | 57 MB + 1016 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 4.224 ms | 11 MB + 104 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 4.225 ms | 11 MB + 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 4.227 ms | 11 MB + 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 507.737 ms | 55 MB + 240 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 508.849 ms | 55 MB + 248 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 738.52 ms | 63 MB + 740 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 738.436 ms | 63 MB + 720 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 738.819 ms | 63 MB + 748 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 5.284 ms | 11 MB + 168 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 5.282 ms | 11 MB + 172 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 738.72 ms | 63 MB + 732 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 737.451 ms | 63 MB + 712 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.26 s | 67 MB + 176 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.264 s | 67 MB + 200 KB | Accepted | Score: 5 | 显示更多 |