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
| #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; const int N=200050*2; const int M=400050; const int INF=0x3f3f3f3f; typedef long long LL; struct Edge {int to,nxt;LL wei;}; struct Array {int x,y;LL w;}; Edge e[M<<1]; Array p[M]; int n,m,f[N][31]; int h[N],cnt,fa[N]; int n_,q,T; int L[N],R[N],k; LL v[N],lastans,val[N],len; bool cmp(Array a,Array b) {return a.w>b.w;} void Add_Edge(int x,int y,int z) {e[++cnt]=(Edge){y,h[x],z};h[x]=cnt;} int getf(int x) {return fa[x]==x?x:fa[x]=getf(fa[x]);} void Clear() { memset(f,0,sizeof(f)); memset(L,0,sizeof(L)); memset(R,0,sizeof(R)); memset(val,0x3f,sizeof(val)); memset(v,0,sizeof(v)); memset(h,0,sizeof(h)); cnt=lastans=0; } int Ex_Kruskal() { sort(p+1,p+m+1,cmp); int ind=n_,lim=n_*2; for (int i=1;i<=lim;i++) fa[i]=i; for (int i=1;i<=m;i++) { int x=getf(p[i].x),y=getf(p[i].y); if (x!=y) { fa[x]=fa[y]=++ind; L[ind]=x,R[ind]=y; val[ind]=p[i].w; if (ind==n_*2-1) break; } } return ind; } struct Node { LL d; int x; Node(LL d=0,int x=0):d(d),x(x){} }; bool operator<(const Node&a,const Node&b) {return a.d>b.d;} LL dis[N]; int vis[N]; void Dijkstra(int s) { fill(dis,dis+N,1e17);dis[s]=0; memset(vis,0,sizeof(vis)); priority_queue<Node> q; q.push(Node(0,s)); while (!q.empty()) { int x=q.top().x; q.pop(); if (vis[x]) continue; vis[x]=1; for (int i=h[x];i;i=e[i].nxt) { int y=e[i].to; if (dis[y]>dis[x]+e[i].wei&&!vis[y]) { dis[y]=dis[x]+e[i].wei; q.push(Node(dis[y],y)); } } } } void Dfs(int x) { if (!L[x]&&!R[x]) { v[x]=dis[x]; return; } f[L[x]][0]=x,f[R[x]][0]=x; Dfs(L[x]),Dfs(R[x]); v[x]=min(v[L[x]],v[R[x]]); } int Find(int x,int d) { for (int i=30;~i;i--) if (f[x][i]&&val[f[x][i]]>d) x=f[x][i]; return x; } void Read() { scanf("%d%d",&n_,&m); for (int i=1;i<=n_;i++) val[i]=INF; for (int i=1;i<=m;i++) { int u,v,w,l; scanf("%d%d%d%d",&u,&v,&l,&w); p[i]=(Array){u,v,w}; Add_Edge(u,v,l); Add_Edge(v,u,l); } scanf("%d%d%lld",&q,&k,&len); } void Init() { n=Ex_Kruskal(); Dijkstra(1); Dfs(n); for (int j=1;(1<<j)<=n;j++) for (int i=1;i<=n;i++) f[i][j]=f[f[i][j-1]][j-1]; } void Solve() { while (q--) { int u; LL d; scanf("%d%lld",&u,&d); u=(u+k*lastans-1)%n_+1; d=(d+k*lastans)%(len+1); printf("%lld\n",lastans=v[Find(u,d)]); } } int main() { scanf("%d",&T); while (T--) { Clear(); Read(); Init(); Solve(); } return 0; }
|