题意:给出一个$N$个点、$M$条边的图,每条边有长度和海拔,$Q$组询问,每一次询问从$v$开始,经过海拔超过$p$的边所能到达的所有点中到点$1$的最短路的最小值,强制在线。$N \leq 2 \times 10^5 , M , Q \leq 4 \times 10^5$
关于$SPFA...$
与边的权值有关的连通块问题,经常可以考虑到$Kruskal$重构树。我们以海拔从大到小构建$Kruskal$重构树,那么对于每一次询问,可以到达的点就对应$Kruskal$重构树上的一棵子树。我们对于每一个点记录它的子树的叶子节点的最短路的最小值,每一次倍增找到询问对应的那一棵子树就能得到答案了。
#include<bits/stdc++.h> //This code is written by Itst using namespace std; inline int read(){ ; ; char c = getchar(); while(c != EOF && !isdigit(c)){ if(c == '-') f = ; c = getchar(); } while(c != EOF && isdigit(c)){ a = (a << ) + (a << ) + (c ^ '); c = getchar(); } return f ? -a : a; } ; struct edge{ int start , end , hei; }now[MAXN]; struct Edge{ int end , upEd , w; }Ed[MAXN << ]; ] , jump[MAXN << ][] , val[MAXN << ] , ans[MAXN << ] , ch[MAXN << ][] , cntNode , cntEd , N , M , Q; priority_queue < pair < int , int > > q; bool operator <(edge a , edge b){ return a.hei > b.hei; } int find(int a){ return fa[a] == a ? a : (fa[a] = find(fa[a])); } inline void addEd(int a , int b , int c){ Ed[++cntEd].end = b; Ed[cntEd].upEd = head[a]; Ed[cntEd].w = c; head[a] = cntEd; } void Dijk(){ memset(minDis , 0x7f , sizeof(minDis)); minDis[] = ; q.push(make_pair( , )); while(!q.empty()){ pair < int , int > t = q.top(); q.pop(); if(-t.first > minDis[t.second]) continue; for(int i = head[t.second] ; i ; i = Ed[i].upEd) if(minDis[Ed[i].end] > minDis[t.second] + Ed[i].w){ minDis[Ed[i].end] = minDis[t.second] + Ed[i].w; q.push(make_pair(-minDis[Ed[i].end] , Ed[i].end)); } } } void dfs(int node){ if(!node) return; ; i <= && jump[node][i - ] ; ++i) jump[node][i] = jump[jump[node][i - ]][i - ]; dfs(ch[node][]); dfs(ch[node][]); } inline int jumpToAll(int x , int h){ ; i >= ; --i) if(val[jump[x][i]] > h) x = jump[x][i]; return x; } int main(){ #ifndef ONLINE_JUDGE freopen("4768.in" , "r" , stdin); //freopen("4768.out" , "w" , stdout); #endif for(int T = read() ; T ; --T){ memset(jump , , sizeof(jump)); memset(ch , , sizeof(ch)); memset(head , , sizeof(head)); cntEd = ; N = read(); M = read(); ; i <= M ; i++){ int a = read() , b = read() , c = read() , d = read(); now[i].start = a; now[i].end = b; now[i].hei = d; addEd(a , b , c); addEd(b , a , c); } Dijk(); ; i <= N ; i++){ fa[i] = i; ans[i] = minDis[i]; } sort(now + , now + M + ); cntNode = N; ; i <= M ; ++i) if(find(now[i].start) != find(now[i].end)){ int a = find(now[i].start) , b = find(now[i].end); fa[a] = fa[b] = jump[a][] = jump[b][] = ++cntNode; ch[cntNode][] = a; ch[cntNode][] = b; val[cntNode] = now[i].hei; ans[cntNode] = min(ans[a] , ans[b]); fa[cntNode] = cntNode; } dfs(cntNode); , Q = read() , K = read() , S = read(); while(Q--){ int a = read() , b = read(); a = (0ll + a + K * lastans - ) % N + ; b = (0ll + b + K * lastans) % (S + ); int t = jumpToAll(a , b); printf("%d\n" , lastans = ans[t]); } } ; }