1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(char c) { putchar(c); } template<> inline void write(char *s) { while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s) { while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1) { write(x),write(x1...); } template<class T> inline void checkMax(T &x,T y) { x=x>y?x:y; } template<class T> inline void checkMin(T &x,T y) { x=x<y?x:y; } const int MAXN=2e5+10,MAXM=4e5+10; int Test,N,M,Q,K,S,Val[MAXN<<1]; struct G { int next,to,val; }Edge[MAXM<<1]; struct Edges { int u,v,h; }Ed[MAXM]; int Head[MAXN<<1],Total; inline void addEdge(int u,int v,int w) { Edge[++Total]=(G){Head[u],v,w};Head[u]=Total; Edge[++Total]=(G){Head[v],u,w};Head[v]=Total; } inline void init() { memset(Head,0,sizeof(Head)),Total=0; read(N,M); for(int i=1,u,v,l,h;i<=M;++i) { read(u,v,l,h);addEdge(u,v,l); Ed[i]=(Edges){u,v,h}; } read(Q,K,S); } struct Que { int x,d; Que(int x=0,int d=0):x(x),d(d){} bool operator<(const Que &x) const { return x.d<d; } }; int Dist[MAXN]; bool Vis[MAXN]; inline void Dijkstra(int st) { std::priority_queue<Que>Q; Q.push(Que(st,0)); memset(Dist,0x3f,sizeof(Dist)),Dist[st]=0; memset(Vis,0,sizeof(Vis)); while(!Q.empty()) { int u=Q.top().x;Q.pop(); if(Vis[u]) continue; Vis[u]=1; for(int e=Head[u],v;e;e=Edge[e].next) { v=Edge[e].to; if(Dist[v]>Dist[u]+Edge[e].val) { Dist[v]=Dist[u]+Edge[e].val; if(!Vis[v]) Q.push(Que(v,Dist[v])); } } } } int Idx; struct Dsu { int Rt[MAXN<<1]; inline void init(int n) { for(int i=1;i<=2*n;++i) Rt[i]=i; } inline int getRt(int x) { return Rt[x]==x?x:Rt[x]=getRt(Rt[x]); } inline int merge(int x,int y) { int p=getRt(x),q=getRt(y); Rt[p]=Rt[q]=++Idx; return Idx; } inline bool connect(int x,int y) { int p=getRt(x),q=getRt(y); return p==q; } }Dsu; inline bool cmp(Edges a,Edges b) { return a.h>b.h; } inline void Kruskal() { memset(Head,0,sizeof(Head)),Total=0; Idx=N; std::sort(Ed+1,Ed+M+1,cmp); Dsu.init(N); for(int i=1;i<=M;++i) { auto e=Ed[i]; int p1=Dsu.getRt(e.u),p2=Dsu.getRt(e.v); if(p1==p2) continue; int lca=Dsu.merge(p1,p2);Val[lca]=e.h; addEdge(lca,p1,0),addEdge(lca,p2,0); } } int Dp[MAXN<<1],Mul[MAXN<<1][25]; void dfsTree(int x,int last) { Dp[x]=Dist[x]; for(int e=Head[x],v;e;e=Edge[e].next) { if((v=Edge[e].to)==last) continue; dfsTree(v,x); Mul[v][0]=x; checkMin(Dp[x],Dp[v]); } } inline void Multi() { for(int i=1;i<25;++i) for(int x=1;x<=Idx;++x) Mul[x][i]=Mul[Mul[x][i-1]][i-1]; } inline int getLc(int x,int val) { for(int i=24;i>=0;--i) if(Val[Mul[x][i]]>val) x=Mul[x][i]; return x; } int main() { read(Test); while(Test--) { init(); Dijkstra(1),Kruskal(); dfsTree(Idx,0),Multi(); int lastans=0; for(int st,s;Q--;) { read(st,s); st=(st+K*lastans-1)%N+1,s=(s+K*lastans)%(S+1); write(lastans=Dp[getLc(st,s)],'\n'); } } return 0; }
|