#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cctype>
#include <cassert>
using namespace std;
const int N=200010,M=400010;
int ne[M<<1],k,fi[N],b[M<<1],c[M<<1],hei[M<<1];
int n,m,q,s,f[N];
void add(int x,int y,int l,int a){
ne[++k]=fi[x]; b[fi[x]=k]=y; c[k]=l; hei[k]=a;
}
void Dijkstra(int s){
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
for (int i=1; i<=n; ++i) f[i]=2147483647;
f[s]=0; pq.push(make_pair(f[s],s));
while (!pq.empty()){
int x=pq.top().second,y=pq.top().first; pq.pop();
if (f[x]<y) continue;
for (int j=fi[x]; j; j=ne[j])
if (f[b[j]]>f[x]+c[j]){
f[b[j]]=f[x]+c[j];
pq.push(make_pair(f[b[j]],b[j]));
}
}
}
namespace Subtask1{
bool in[N];
int v,p;
int bfs(int v,int p){
for (int i=1; i<=n; ++i) in[i]=0;
queue<int> q; q.push(v); in[v]=1;
while (!q.empty()){
int x=q.front(); q.pop();
for (int j=fi[x]; j; j=ne[j])
if (hei[j]>p&&!in[b[j]]){
in[b[j]]=1;
q.push(b[j]);
}
}
int ret=f[v];
for (int i=1; i<=n; ++i) if (in[i]) ret=min(ret,f[i]);
return ret;
}
void solve(){
int lastans=0;
for (int i=1; i<=q; ++i){
scanf("%d%d",&v,&p);
v=((long long)v+lastans-1)%n+1;
p=((long long)p+lastans)%(s+1);
printf("%d\n",(lastans=bfs(v,p)));
}
}
}
namespace Subtask2{
int v[N],p[N],ans[N],id[N],id2[M],fa[N];
bool cmp(int x,int y){
return p[x]>p[y];
}
bool cmp2(int x,int y){
return hei[x]>hei[y];
}
int ask(int x){
return x==fa[x]?x:fa[x]=ask(fa[x]);
}
void unite(int x,int y){
x=ask(x); y=ask(y);
if (x==y) return;
if (f[x]<f[y]) fa[y]=x;
else fa[x]=y;
}
void solve(){
for (int i=1; i<=q; ++i){
scanf("%d%d",&v[i],&p[i]);
id[i]=i;
}
sort(id+1,id+q+1,cmp);
for (int i=2; i<=k; i+=2) id2[i>>1]=i;
sort(id2+1,id2+(k>>1)+1,cmp2);
int ne=1;
for (int i=1; i<=n; ++i) fa[i]=i;
for (int i=1; i<=q; ++i){
while (ne<=k>>1&&hei[id2[ne]]>p[id[i]]){
unite(b[id2[ne]],b[id2[ne]-1]);
++ne;
}
ans[id[i]]=f[ask(v[id[i]])];
}
for (int i=1; i<=q; ++i) printf("%d\n",ans[i]);
}
}
namespace fastIO{
const int SIZE=1e6;
char buff[SIZE];
char *l=buff,*r=buff;
void init(){
l=buff;
r=l+fread(buff,1,SIZE,stdin);
}
char gc(){
if (l==r) init();
return *(l++);
}
void read(int &x){
char ch=gc(); x=0;
while (!isdigit(x)) ch=gc();
while (isdigit(x)) x=(x<<3)+(x<<1)+(ch^48),ch=gc();
}
}
namespace Subtask3{
#define M int mid=(l+r)>>1;
#define L (st[ind].l,l,mid)
#define R (st[ind].r,mid+1,r)
int fa[N],sz[N],id[N<<1],root[N<<1],pos,now,v,p,tot,cp;
struct node{
int f,l,r;
}st[N*60];//care
bool cmp(int x,int y){
return hei[x]>hei[y];
}
bool cmp2(int x,int y){
return x>y;
}
int ask(int ind,int l,int r){
if (l==r) return st[ind].f;
M;
return pos<=mid?ask L:ask R;
}
int finds(int x){
pos=x;
int t=ask(root[now],1,n);
// cerr<<"fins"<<t<<endl;
return t<=0?-t:finds(t);
}
int df(int v,int p){
now=lower_bound(id+1,id+(k>>1)+1,p,cmp2)-id-1;
// cerr<<"now"<<now<<endl;
return finds(v);
}
void modify(int &ind,int l,int r){
// if (tot+1==N*70) assert(0);
st[ind=++tot]=st[cp];
// cerr<<"modify"<<ind<<" "<<cp<<" "<<l<<" "<<r<<endl;
if (l==r){
st[ind].f=v;
return;
}
M;
pos<=mid?(cp=st[cp].l,modify L):(cp=st[cp].r,modify R);
}
int find(int x){
return fa[x]<=0?x:find(fa[x]);
}
void fake(int x,int y,int i){
x=find(x); y=find(y);
if (x==y){
root[i]=root[i-1];
return;
}
if (sz[x]<sz[y]) swap(x,y);
fa[x]=max(fa[x],fa[y]);
fa[y]=x;
v=fa[y]; pos=y; cp=root[i-1];
modify(root[i],1,n);
v=fa[x]; pos=x; cp=root[i];
modify(root[i],1,n);
sz[x]+=sz[y];
// for (int i=1; i<=n;++i) cerr<<fa[i]<<" ";
}
void prework(){
for (int i=1; i<=tot; ++i) st[i].f=st[i].l=st[i].r=0;
tot=0;
for (int i=1; i<=n; ++i){
fa[i]=-f[i];
v=fa[i]; pos=i; cp=root[0];
modify(root[0],1,n);
sz[i]=1;
}
for (int i=2; i<=k; i+=2) id[i>>1]=i;
sort(id+1,id+(k>>1)+1,cmp);
for (int i=1; i<=(k>>1); ++i) fake(b[id[i]],b[id[i]-1],i);
for (int i=1; i<=(k>>1); ++i) id[i]=hei[id[i]];
}
void solve(int k){
prework();
int lastans=0;
for (int i=1; i<=q; ++i){
scanf("%d%d",&v,&p);
v=((long long)v+k*lastans-1)%n+1;
p=((long long)p+k*lastans)%(s+1);
printf("%d\n",(lastans=df(v,p)));
}
}
}
int main(){
freopen("return.in","r",stdin);
freopen("return.out","w",stdout);
int t;
scanf("%d",&t);
while (t--){
scanf("%d%d",&n,&m);
for (int i=1; i<=n; ++i) fi[i]=0; k=0;
for (int i=1; i<=m; ++i){
int x,y,l,a;
scanf("%d%d%d%d",&x,&y,&l,&a);
add(x,y,l,a); add(y,x,l,a);
}
Dijkstra(1);
int k;
scanf("%d%d%d",&q,&k,&s);
/*if (k==1&&n>1500) Subtask3::solve();
else*/
//if (k==1)
Subtask3::solve(k);
//else Subtask2::solve();
}
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 16.94 us | 52 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 46.6 us | 68 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 222 us | 96 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 372.55 us | 112 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 4.963 ms | 896 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 938.826 ms | 148 MB + 228 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 4.133 ms | 804 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 4.122 ms | 808 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 4.156 ms | 804 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 1.068 s | 141 MB + 724 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 1.064 s | 141 MB + 728 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 1.224 s | 147 MB + 272 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 1.226 s | 148 MB + 212 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 1.224 s | 148 MB + 196 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 5.99 ms | 892 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #16 | 5.983 ms | 892 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #17 | 1.225 s | 148 MB + 308 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #18 | 1.228 s | 148 MB + 248 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #19 | 2.239 s | 150 MB + 728 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #20 | 2.228 s | 151 MB + 808 KB | Accepted | Score: 5 | 显示更多 |