#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpi;
namespace IAOI_lib{
template<typename T> class dsu{
private:
vector<T> a;
vector<int> s;
public:
dsu(int n){
a.resize(n),s.resize(n);
iota(a.begin(),a.end(),0);
fill(s.begin(),s.end(),1);
}
T leader(T x){
return a[x]==x?x:a[x]=leader(a[x]);
}
int size(T x){
return s[leader(x)];
}
void merge(T f,T x,T y){
x=leader(x),y=leader(y);
s[a[x]=a[y]=f]=s[x]+s[y]+1;
}
bool same(T x,T y){
return leader(x)==leader(y);
}
};
}
main(){
ios::sync_with_stdio(false);
int t; cin>>t;
while(t--){
int n,m,c,lst=0; cin>>n>>m,c=n;
vector<vector<tpi> > g(n);
vector<tpi> e(n);
for(int i=0;i<m;i++){
int u,v,l,a; cin>>u>>v>>l>>a;
e.emplace_back(a,--u,--v);
g[u].emplace_back(v,l,a);
g[v].emplace_back(u,l,a);
}
vector<pii> f(n<<1,make_pair(1e16,0));
vector<bool> b(n);
priority_queue<pii,vector<pii>,greater<> > Q;
Q.emplace(f[0].first=0,0);
while(!Q.empty()){
int u=Q.top().second; Q.pop();
if(b[u])continue; b[u]=true;
for(auto [i,l,a]:g[u])
if(f[i].first>f[u].first+l)
Q.emplace(f[i].first=f[u].first+l,i);
}
sort(e.begin(),e.end(),greater<>());
IAOI_lib::dsu<int> mst(n<<1);
vector<vector<int> > t(n<<1);
for(auto [w,u,v]:e)
if(!mst.same(u,v)){
t[c].emplace_back(mst.leader(u));
t[c].emplace_back(mst.leader(v));
mst.merge(c,u,v);
f[c].second=w;
if(++c==n<<1)break;
}
vector<int> d(n<<1);
int l=__lg(n)+1; vector s(n<<1,vector<int>(l+1));
function<void(int,int)> dfs=[&](int u,int r){
s[u][0]=r; for(int i=1;i<=l;i++)
s[u][i]=s[s[u][i-1]][i-1];
for(int i:t[u])
dfs(i,u),f[u].first=min(f[u].first,f[i].first);
};
dfs(n-1<<1,n-1<<1);
int q,k,h; cin>>q>>k>>h;
while(q--){
int v,p; cin>>v>>p;
(v+=k*lst-1)%=n,(p+=k*lst)%=h+1;
for(int i=l;~i;i--)
if(f[s[v][i]].second>p)v=s[v][i];
cout<<(lst=f[v].first)<<'\n';
}
}
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 46.08 us | 56 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 82.89 us | 64 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 342.57 us | 96 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 534.53 us | 136 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 6.767 ms | 1 MB + 140 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 1.374 s | 150 MB + 628 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 5.072 ms | 992 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 5.168 ms | 1000 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 4.987 ms | 992 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 1.128 s | 132 MB + 36 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 1.132 s | 132 MB + 40 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 1.519 s | 155 MB + 620 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 1.52 s | 155 MB + 608 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 1.522 s | 155 MB + 624 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 7.254 ms | 1 MB + 168 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #16 | 7.287 ms | 1 MB + 164 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #17 | 1.523 s | 155 MB + 592 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #18 | 1.519 s | 155 MB + 608 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #19 | 2.226 s | 159 MB + 60 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #20 | 2.224 s | 159 MB + 76 KB | Accepted | Score: 5 | 显示更多 |