洛谷P4768 [NOI2018]歸程(Kruskal重構樹)

題意

直接看題目吧,很差描述函數

Sol

考慮暴力作法大數據

首先預處理出從$1$到每一個節點的最短路,優化

對於每次詢問,暴力的從這個點BFS,從能走到的點裏面取$min$spa

考慮如何優化,這裏要用到Kruskal重構樹code

咱們按邊權的海拔從大到小排序,建出Kruskal重構樹blog

這必定是一個小根堆排序

那麼一個點的子樹內的節點必定能夠相互到達且通過的最小的海拔爲該點權值get

那麼每次查詢的時候,咱們只須要倍增的處理出從這個點向上走多少纔不能知足條件string

而後在子樹內查每一個點到$1$的最大值便可。it

 

哎,調了一上午也沒調出來,只有72分,能夠過全部的單個數據,可是一塊兒跑就GG,並且我本機跑大數據會RE。

繼續調下去也沒啥意思了,等哪天心情好了重寫一遍吧。

這題直接該變我三觀啊qwq。

我先是把邊改爲從$1$開始,而後就變成了$70$分。提示第$3$個點輸出$0$,可是我本機能夠過

而後我又把比較函數裏的$=$去了就A了。

why????????

// luogu-judger-enable-o2
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
//#define int long long 
#define MP(x, y) make_pair(x, y)
#define Pair pair<int, int> 
using namespace std;
const int MAXN = 1e6 + 10, INF = 2147483646, B = 18;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = 1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int QwQ, N, M;
struct Edge {
    int u, v, l, a, nxt;
    bool operator < (const Edge &rhs) const {
        return a > rhs.a;//小根堆 
    }
}E[MAXN << 1];
int head[MAXN], num;
inline void AddEdge(int x, int y, int z, int gg) {
    E[num] = (Edge){x, y, z, gg, head[x]};
    head[x] = num++;
}
inline void work() {
    N = read(); M = read();
    for(int i = 1; i <= M; i++) {
        int u = read(), v = read(), l = read(), a = read();
        AddEdge(u, v, l, a); AddEdge(v, u, l, a);
    }
}

int dis[MAXN];
void MinDisRood() {//求出從1到各個節點的最短路 
    memset(dis, 0x3f, sizeof(dis));
    priority_queue<Pair> q;
    dis[1] = 0; q.push(MP(0, 1));
    while(!q.empty()) {
        int p = q.top().second; q.pop();
        for(int i = head[p]; i != -1; i = E[i].nxt) {
            int to = E[i].v;
            if(dis[to] > dis[p] + E[i].l) 
                dis[to] = dis[p] + E[i].l, q.push(MP(-dis[to], to));
        }
    }
}
int fa[MAXN], cnt, g[MAXN][21], f[MAXN][21], ch[MAXN][21], val[MAXN];
int find(int x) {
    if(fa[x] == x) return fa[x];
    return fa[x] = find(fa[x]);
}
void Kruskal() {
    cnt = N;
    for(int i = 0; i <= 2 * N + 1; i++)  fa[i] = i;
    sort(E + 1, E + num);
    for(int i = 1; i < num; i++) {
        int fx = find(E[i].u), fy = find(E[i].v);
        if(fx == fy) continue;
        ch[++cnt][0] = fx; ch[cnt][1] = fy;
        fa[fx] = fa[fy] = f[fx][0] = f[fy][0] = cnt;
        g[fx][0] = g[fy][0] = E[i].a;
    }
}
int mi[MAXN];
int dfs(int x) {
    mi[x] = INF;
    if(!ch[x][0] || !ch[x][1]) return mi[x] = dis[x];
    mi[x] = min(mi[x], dfs(ch[x][0]));
    mi[x] = min(mi[x], dfs(ch[x][1]));
    return mi[x];
}
void Pre() {
    for(int j = 1; j <= B; j++) 
        for(int i = 1; i <= 2 * N; i++)
            f[i][j] = f[f[i][j - 1]][j - 1], 
            g[i][j] = min(g[i][j - 1], g[f[i][j - 1]][j - 1]);    
    dfs(cnt);
}
int Find(int bg, int val) {//從bg出發網上跳,找到海拔大於val的最小的點 
    for(int i = B; i >= 0; i--)
        if(g[bg][i] > val && g[bg][i] <= INF)
            bg = f[bg][i];
    return bg;
}
void DealQuery() {
    int Q = read(), K = read(), S = read(), lastans = 0;
    while(Q--) {
        int v = read(), p = read();
        v = (v + K * lastans - 1) % N + 1;
        p = (p + K * lastans) % (S + 1);
        int tot = 0, pos = Find(v, p);   
        printf("%d\n", lastans = mi[pos]);

    }
}
inline void init() {
    memset(g, 0x3f, sizeof(g));
    memset(val, 0, sizeof(val));
    memset(head, -1, sizeof(head)); 
    memset(ch, 0, sizeof(ch));
    memset(f, 0, sizeof(f));
    num = 1;
}
main() {
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    QwQ = read();
    while(QwQ--) {
        init();
        work();
        //if(N != 1500 || M != 2922) continue;
        MinDisRood();
        Kruskal();
        Pre();
        DealQuery();
    }
    return 0;
}
相關文章
相關標籤/搜索