#define _CRT_SECURE_NO_DEPRECATE
/************************
*创建时间:2018 10 09
*文件类型:源代码文件
*题目来源:洛谷
*当前状态:已通过
*备忘录:图论 最小生成树 kruskal重构树
*作者:HtBest
************************/
#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <bitset>
// #include <sys/wait.h>
// #include <sys/types.h>
// #include <unistd.h>
using namespace std;
#define MAXN 200001
#define MAXM 400001
int n,m,q,k,s,_edge,head[MAXN],d[MAXN],//基本
son[2*MAXN],fa[2*MAXN],bro[2*MAXN],h[2*MAXN],f[2*MAXN],_node,//kruskal重构树
mind[2*MAXN],up[2*MAXN][20];//统计答案
struct EDGE
{
int a,b,v,h,next;
EDGE(int a=0,int b=0,int v=0,int h=0,int next=0):a(a),b(b),v(v),h(h),next(next){}
bool operator < (EDGE x) const
{
return h>x.h;
}
}edge[2*MAXM];
struct A
{
int id,d;
A(int id=0,int d=0):id(id),d(d){}
bool operator < (A x) const
{
return d>x.d;
}
};
/* Variable explain:
n:节点数
m:边数
q:询问数
edge[i]:邻接表
_edge:邻接表标记
h[i]:kruskal重构树上第i个点的海拔
_node:节点标记
f[i]:并查集
son[i]:i的子节点
fa[i]:i的父亲
bro[i]:i的下一个兄弟
*/
void adde(int a,int b,int v,int h)
{
edge[++_edge]=EDGE(a,b,v,h,head[a]);
head[a]=_edge;
}
void addt(int a,int b)
{
// printf("%d<--%d\n",a,b);
fa[b]=a;
bro[b]=son[a];
son[a]=b;
}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void merge(int a,int b){f[find(a)]=find(b);}
void read()
{
int ls1,ls2,ls3,ls4;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)head[i]=0,h[i]=1e9;
for(int i=1;i<=2*n;++i)son[i]=fa[i]=bro[i]=0,mind[i]=1e9;
_edge=0;_node=n;
for(int i=1;i<=m;++i)
{
scanf("%d%d%d%d",&ls1,&ls2,&ls3,&ls4),adde(ls1,ls2,ls3,ls4),adde(ls2,ls1,ls3,ls4);
}
scanf("%d%d%d",&q,&k,&s);
return;
}
void kruskal()
{
sort(edge+1,edge+1+_edge);
for(int i=1;i<=2*n;++i)f[i]=i;
for(int i=1;i<=_edge;++i)
{
int a=edge[i].a,b=edge[i].b;
if(find(a)!=find(b))
{
addt(++_node,find(a));
addt(_node,find(b));
h[_node]=edge[i].h;
merge(a,_node);
merge(b,_node);
}
}
}
void dijkstra()
{
bitset <MAXN> vis;
priority_queue <A> q;
q.push(A(1,0));
for(int i=1;i<=n;++i)d[i]=1e9;
d[1]=0;
while(!q.empty())
{
int a=q.top().id;
q.pop();
if(vis[a])continue;
vis[a]=true;
for(int i=head[a];i;i=edge[i].next)
{
int b=edge[i].b,v=edge[i].v;
if(d[b]>d[a]+v)
{
d[b]=d[a]+v;
q.push(A(b,d[b]));
}
}
}
}
void dfs(int a)
{
for(int i=son[a];i;i=bro[i])
{
dfs(i);
mind[a]=min(mind[a],mind[i]);
// printf("%d(%d) --> %d\n",i,mind[i],a);
}
if(a<=n)mind[a]=d[a];
}
void init()
{
for(int i=1;i<=_node;++i)up[i][0]=fa[i];
for(int j=1;j<20;++j)
for(int i=1;i<=_node;++i)up[i][j]=up[up[i][j-1]][j-1];
}
int solve(int S,int H)
{
for(int i=19;i>=0;--i)
{
if(h[up[S][i]]>H)S=up[S][i];
}
return S;
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
read();
dijkstra();
kruskal();
dfs(_node);
init();
int ans=0;
while(q--)
{
int S,H;
scanf("%d%d",&S,&H);
S=(S+k*ans-1)%n+1;
H=(H+k*ans)%(s+1);
// printf("Q:%d %d\n",S,H);
S=solve(S,H);
printf("%d\n",ans=mind[S]);
// printf("%d %d\n",S,ans=mind[S]);
}
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 2.217 ms | 15 MB + 356 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 2.242 ms | 15 MB + 360 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 2.453 ms | 15 MB + 372 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 2.583 ms | 15 MB + 384 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 6.608 ms | 15 MB + 708 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 723.95 ms | 59 MB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 5.747 ms | 15 MB + 712 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 5.741 ms | 15 MB + 716 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 5.755 ms | 15 MB + 712 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 608.345 ms | 60 MB + 420 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 608.66 ms | 60 MB + 424 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 890.582 ms | 62 MB + 256 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 887.358 ms | 62 MB + 244 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 889.062 ms | 62 MB + 264 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 7.335 ms | 15 MB + 708 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 7.355 ms | 15 MB + 712 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 888.73 ms | 62 MB + 244 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 889.727 ms | 62 MB + 248 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.44 s | 65 MB + 708 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.44 s | 65 MB + 748 KB | Accepted | Score: 5 | 显示更多 |