VIJOS1843 - [NOIP2013]货车运输
不少大佬吐槽这题可以暴力水过。
认真写的话就是 最大生成树 + 倍增LCA。
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <bits/stdc++.h>
using namespace std;
const int MAX_POOL = 1E6;
typedef pair<int, int> PII;
struct tgno {
    int u, v, w;
    tgno* next;
}; 
struct Graph {
    tgno POOL[MAX_POOL]; int ppos;
    tgno* Head[MAX_POOL];
    void _add(int u, int v, int w) {
        POOL[ppos].u = u; POOL[ppos].v = v;
        POOL[ppos].w = w; POOL[ppos].next = Head[u];
        Head[u] = &POOL[ppos++];
    }
    void addEdge(int u, int v, int w) {
        _add(u, v, w); _add(v, u, w);
    }
} G1, G2;
struct djset {
    int Fa[MAX_POOL];
    djset() { for(int i=0;i<MAX_POOL;i++) Fa[i] = i; }
    int find(int x) { return x==Fa[x]?x:Fa[x]=find(Fa[x]); }
    void link(int x, int y) { Fa[find(x)] = find(y); }
};
int N, M, Q;
bool _kcmp(tgno a, tgno b) {
    return a.w > b.w;
}
void kruskal(Graph &src, Graph &dst) {
    sort(src.POOL, src.POOL+src.ppos, _kcmp);
    djset S;
    for(int i=0;i<src.ppos;i++) {
        if(S.find(src.POOL[i].u) != S.find(src.POOL[i].v)) {
            S.link(src.POOL[i].u, src.POOL[i].v);
            dst.addEdge(src.POOL[i].u, src.POOL[i].v, src.POOL[i].w);
        }
    }
}
int vis[MAX_POOL];
int dep[MAX_POOL], pre[MAX_POOL][20], dis[MAX_POOL][20];
void dfs(const Graph &G, int curr) {
    typedef tgno* It;
    for(It i=G.Head[curr];i;i=i->next) {
        if(!dep[i->v]) {
            dep[i->v] = dep[curr] + 1;
            pre[i->v][0] = curr;
            dis[i->v][0] = i->w;
            dfs(G, i->v);
        }
    }
}
void mkLCA(const Graph &G, int root) {
    dep[root] = 1;
    memset(dis, 0x3f, sizeof(dis));
    dfs(G, root);
    for(int j=1;(1<<j)<=N;j++) {
        for(int i=1;i<=N;i++) {
            if(pre[i][j-1]) {
                pre[i][j] = pre[pre[i][j-1]][j-1];
                dis[i][j] = dis[i][j-1];
                if(dis[pre[i][j-1]][j-1])
                    dis[i][j] = min(dis[i][j], dis[pre[i][j-1]][j-1]);
            }
        }
    }
}
// < LCA, Capacity >
PII LCA(int A, int B) {
    int dd = numeric_limits<int>::max(), i, j, a = A, b = B;
    if(dep[a] < dep[b]) swap(a, b);
    for(i=0;(1<<i)<=dep[a];i++);
    i--;
    for(j=i;j>=0;j--)
        if(dep[a]-(1<<j)>=dep[b]) {
            dd = min(dd, dis[a][j]); a = pre[a][j];
        }
    if(a == b) return PII(a, dd);
    for(j=i;j>=0;j--) {
        if(pre[a][j] && pre[a][j] != pre[b][j]) {
            dd = min(dd, dis[a][j]); a = pre[a][j];
            dd = min(dd, dis[b][j]); b = pre[b][j];
        }
    }
    return PII(pre[a][0], min(min(dd, dis[a][0]), dis[b][0]));
}
int main() {
    cin >> N >> M;
    int u, v, w;
    while(M--) {
        cin >> u >> v >> w;
        G1._add(u, v, w);
    }
    kruskal(G1, G2);
    mkLCA(G2, 1);
	
	cin >> Q;
	while(Q--) {
        cin >> u >> v;
        int c = LCA(u, v).second;
        cout << (c==0x3f3f3f3f?-1:c) << endl;
	}
}