題目連結:http://cs.scu.edu.cn/soj/problem.action?id=3098
題目概述:
Description
Once again, James Bond is on his way to saving the world. Bond's latest mission requires
him to travel between several pairs of cities in a certain country.
The country has N cities (numbered by 1, 2, . . ., N), connected by M bidirectional roads.
Bond is going to steal a vehicle, and drive along the roads from city s to city t.
The country's police will be patrolling the roads, looking for Bond, however, not all
roads get the same degree of attention from the police.
More formally, for each road MI6 has estimated its dangerousness, the higher it is,
the more likely Bond is going to be caught while driving on this road. Dangerousness
of a path from s to t is defined as the maximum dangerousness of any road on this path.
Now, it's your job to help Bond succeed in saving the world by finding the least dangerous
paths for his mission.
Input
There will be at most 5 cases in the input file.
The first line of each case contains two integers N, M (2 ≤ N≤ 50000, 1≤ M ≤ 100000) –
number of cities and roads. The next M lines describe the roads. The i-th of these lines
contains three integers: xi, yi, di (1 ≤ xi, yi ≤ N, 0 ≤ di ≤ 10^9) - the numbers of the
cities connected by the ith road and its dangerousness.
Description of the roads is followed by a line containing an integer Q (1 ≤ Q ≤ 50000), followed
by Q lines, the i-th of which contains two integers si and ti (1 ≤ si, ti ≤ N, si != ti).
Consecutive input sets are separated by a blank line.
Output
For each case, output Q lines, the i-th of which contains the minimum dangerousness of a path
between cities si and ti. Consecutive output blocks are separated by a blank line.
The input file will be such that there will always be at least one valid path.
Sample Input
4 5
1 2 10
1 3 20
1 4 100
2 4 30
3 4 10
2
1 4
4 1
2 1
1 2 100
1
1 2
Sample Output
20
20
100
Source
Ivan Krasilnikov @ Next generation contest – 4 @ UVA
解決方案:最小產生樹+lca(可以用tarjan,正是因為我想用rmq結果搞得fuckable了)
代碼:
#include <iostream><br />#include <sstream><br />#include <iomanip><br />#include <vector><br />#include <deque><br />#include <list><br />#include <set><br />#include <map><br />#include <stack><br />#include <queue><br />#include <bitset><br />#include <string><br />#include <algorithm><br />#include <functional><br />#include <cstdio><br />#include <cstring><br />#include <cmath><br />#include <cstdlib><br />#include <cctype><br />#include <complex><br />using namespace std;</p><p>//使用堆最佳化的prim演算法或者kruskal演算法計算最小產生樹<br />//#define PRIM</p><p>//使用稀疏表或者線段樹實現lca-rmq<br />//#define SPARSE_TABLE</p><p>typedef long long int64;<br />typedef pair<int, int> Pt;<br />#define pb push_back<br />#define mp make_pair<br />const int MAX_NODE = 50005;</p><p>#ifdefPRIM</p><p>//堆最佳化<br />int heap_pos[MAX_NODE];<br />int heap[MAX_NODE];<br />int heap_count;</p><p>//prim演算法基本資料<br />vector<Pt> prim_adj[MAX_NODE];<br />int d[MAX_NODE];<br />int mark[MAX_NODE];<br />int from[MAX_NODE];</p><p>//prim演算法輸出<br />vector<Pt> mst[MAX_NODE];</p><p>//LCA基本資料<br />#define lca_edges mst<br />intlca_lev[MAX_NODE];<br />intlca_E[MAX_NODE<<1], lca_time[MAX_NODE<<1], array[MAX_NODE<<1][18];<br />int lca_start[MAX_NODE], lca_end[MAX_NODE];<br />intlca_id, lca_curr_time;</p><p>//查詢的表<br />Ptst[MAX_NODE][18];</p><p>inline void adjust(int root)<br />{<br />int value = d[heap[root]];<br />int x = heap[root];<br />int j;<br />for (j = 2*root; j <= heap_count; j <<= 1)<br />{<br />if (j < heap_count && d[heap[j+1]] < d[heap[j]]) ++j;<br />if (value <= d[heap[j]]) break;<br />heap[j>>1] = heap[j];<br />heap_pos[heap[j>>1]] = j>>1;<br />}<br />heap[j>>1] = x;<br />heap_pos[heap[j>>1]] = j>>1;<br />}</p><p>inline void adjust_up(int root)<br />{<br />int value = d[heap[root]];<br />int x = heap[root];<br />for (; root>>1; root >>= 1)<br />{<br />if (d[heap[root>>1]] <= value) break;<br />heap[root] = heap[root>>1];<br />heap_pos[heap[root]] = root;<br />}<br />heap[root] = x;<br />heap_pos[heap[root]] = root;<br />}</p><p>inline void build_heap()<br />{<br />adjust(1);<br />}</p><p>inline int exact_min()<br />{<br />int pos = heap[1];<br />heap_pos[heap[heap_count]] = 1;<br />heap[1] = heap[heap_count--];<br />adjust(1);<br />return pos;<br />}</p><p>inline int fast_prim(int start, int n, int e_count = 0)<br />{<br />int ans = 0;<br />for (int i = 1; i <= n; ++i) d[i] = 2000000000, heap_pos[i] = heap[i] = from[i] = i;<br />for (int i = 1; i <= n; ++i) mst[i].clear();<br />d[start] = 0;<br />heap_count = n;<br />build_heap();<br />for (int i = 0; i < n; ++i)<br />{<br />int pos = exact_min();<br />ans += d[pos];<br />{<br />int x = from[pos];<br />if (x != pos)<br />{<br />mst[pos].push_back(make_pair(x, d[pos]));<br />mst[x].push_back(make_pair(pos, d[pos]));<br />}<br />}<br />int size = prim_adj[pos].size();<br />for (int j = 0; j < size; ++j)<br />{<br />int next = prim_adj[pos][j].first;<br />int w = prim_adj[pos][j].second;<br />if (w < d[next])<br />{<br />d[next] = w;<br />from[next] = pos;<br />adjust_up(heap_pos[next]);<br />}<br />}<br />}<br />return ans;<br />}</p><p>#definecal_mst fast_prim</p><p>#else</p><p>//並查集的資料<br />int p[MAX_NODE];</p><p>//knuscal演算法基本資料<br />struct Tri<br />{<br />int s, e, w;<br />};<br />Triedges[MAX_NODE<<1];<br />vector<Pt> mst[MAX_NODE];</p><p>//LCA基本資料</p><p>#define lca_edges mst<br />intlca_lev[MAX_NODE];<br />intlca_E[MAX_NODE<<1], lca_time[MAX_NODE<<1], array[MAX_NODE<<1][18];<br />intlca_start[MAX_NODE], lca_end[MAX_NODE];<br />intlca_id, lca_curr_time;</p><p>//查詢的表<br />Ptst[MAX_NODE][18];</p><p>inline void link(int x, int y)<br />{<br />if (x == y) return;<br />if (p[x] < p[y]) p[y] = x;<br />else<br />{<br />int t = p[y] == p[x];<br />p[x] = y;<br />p[y] -= t;<br />}<br />}</p><p>inline int find(int x)<br />{<br />int r = x;<br />while (p[r] > 0) r = p[r];<br />while (x != r)<br />{<br />int t = p[x]; p[x] = r; x = t;<br />}<br />return r;<br />}</p><p>inline void merge(int x, int y)<br />{<br />link(find(x), find(y));<br />}</p><p>inline int cmp_by_weight(const Tri& x, const Tri& y)<br />{<br />return x.w < y.w;<br />}</p><p>inline void kruskal(int start, int n, int m)<br />{<br />int value = 0;<br />sort(edges, edges+m, cmp_by_weight);<br />memset(p, 0, sizeof p);<br />for (int i = 1; i <= n; ++i) mst[i].clear();<br />int have = 0;<br />for (int i = 0; have < n - 1 && i < m; ++i)<br />{<br />int x = find(edges[i].s);<br />int y = find(edges[i].e);<br />if (x == y) continue;<br />mst[edges[i].s].pb(mp(edges[i].e, edges[i].w));<br />mst[edges[i].e].pb(mp(edges[i].s, edges[i].w));<br />merge(x, y);<br />value += edges[i].w;<br />++have;<br />}<br />}</p><p>#definecal_mst kruskal</p><p>#endif</p><p>void lca_dfs(int root, int parent, int level)<br />{<br />int temp = lca_curr_time++;<br />lca_lev[root] = level;<br />lca_start[root] = lca_id;<br />int size = lca_edges[root].size();<br />for (int i = 0; i < size; ++i)<br />{<br />int next = lca_edges[root][i].first;<br />if (next == parent) continue;<br />lca_E[lca_id] = root;<br />lca_time[lca_id++] = temp;<br />st[next][0] = make_pair(root, lca_edges[root][i].second);<br />lca_dfs(next, root, level+1);<br />}<br />lca_end[root] = lca_id;<br />lca_E[lca_id] = root;<br />lca_time[lca_id++] = temp;<br />}</p><p>#ifdef SPARSE_TABLE<br />//稀疏表實現的RMQ<br />inline int RMQ(int id1, int id2)<br />{<br />++id2;<br />int ii = (int)(log(double(id2-id1))/log(2.0));<br />if (lca_time[array[id1][ii]] < lca_time[array[id2-(1<<ii)][ii]])<br />return lca_E[array[id1][ii]];<br />return lca_E[array[id2-(1<<ii)][ii]];<br />}</p><p>#define start lca_start<br />#define end lca_end<br />inline int LCA(int id1, int id2)<br />{<br />if (start[id1] < start[id2] && end[id2] < end[id1]) return id1;<br />if (start[id2] < start[id1] && end[id1] < end[id2]) return id2;<br />if (start[id1] > end[id2]) return RMQ(end[id2], start[id1]);<br />return RMQ(end[id1], start[id2]);<br />}</p><p>#else<br />//線段樹實現的RMQ<br />int lca_tree[MAX_NODE<<3];<br />int lca_L[MAX_NODE<<1];<br />inline void lca_build_tree(int root, int l, int r)<br />{<br />if (l == r)<br />{<br />lca_tree[root] = l;<br />return;<br />}<br />int mid = (l+r)>>1, lc = root << 1, rc = lc + 1;<br />lca_build_tree(lc, l, mid);<br />lca_build_tree(rc, mid+1, r);<br />if (lca_L[lca_tree[lc]] < lca_L[lca_tree[rc]]) lca_tree[root] = lca_tree[lc];<br />else lca_tree[root] = lca_tree[rc];<br />}</p><p>inline int lca_query(int root, int l, int r, int a, int b)<br />{<br />if (l == a && r == b) return lca_tree[root];<br />int mid = (l+r)>>1, lc = root << 1, rc = lc + 1;<br />if (b <= mid) return lca_query(lc, l, mid, a, b);<br />else if (a > mid) return lca_query(rc, mid+1, r, a, b);<br />int x = lca_query(lc, l, mid, a, mid);<br />int y = lca_query(rc, mid+1, r, mid+1, b);<br />return lca_L[x] < lca_L[y] ? x : y;<br />}</p><p>inlineintLCA(int u, int v)<br />{<br />int r1 = lca_start[u], r2 = lca_start[v], t;<br />if (r1 <= r2) t = lca_query(1, 1, lca_id-1, r1, r2);<br />else t = lca_query(1, 1, lca_id-1, r2, r1);<br />return lca_E[t];<br />}<br />#endif</p><p>inlinevoidlca_init(int root, int n)<br />{<br />lca_id = 1, lca_curr_time = 1;<br />lca_dfs(root, -1, 1);<br />#ifdefSPARSE_TABLE<br />for (int i = 1; i < lca_id; ++i) array[i][0] = i;<br />for (int k = 1; (1<<k) < lca_id; ++k) for (int i = 1; i + (1<<k) <= lca_id; ++i)<br />if (lca_time[array[i][k-1]] < lca_time[array[i+(1<<(k-1))][k-1]])<br />array[i][k] = array[i][k-1];<br />else<br />array[i][k] = array[i+(1<<(k-1))][k-1];<br />#else<br />for (int i = 1; i < lca_id; ++i) lca_L[i] = lca_lev[lca_E[i]];<br />lca_build_tree(1, 1, lca_id-1);<br />#endif<br />}</p><p>inline int query(int id1, int id2)<br />{<br />int ans = 0;<br />while (lca_lev[id1] > lca_lev[id2])<br />{<br />int i;<br />for (i = 0; lca_lev[id1]-(1<<i) >= lca_lev[id2]; ++i);<br />--i;<br />if (st[id1][i].second > ans) ans = st[id1][i].second;<br />id1 = st[id1][i].first;<br />}<br />return ans;<br />}</p><p>int main()<br />{<br />int n, m;<br />int first = 1;<br />while (scanf("%d%d", &n, &m) == 2)<br />{<br />if (first == 1) first = 2; else puts("");<br />#ifdefPRIM<br />for (int i = 1; i <= n; ++i) prim_adj[i].clear();<br />for (int i = 0; i < m; ++i)<br />{<br />int s, e, v;scanf("%d%d%d", &s, &e, &v);<br />prim_adj[s].push_back(make_pair(e, v));<br />prim_adj[e].push_back(make_pair(s, v));<br />}<br />#else<br />for (int i = 0; i < m; ++i)<br />{<br />scanf("%d%d%d", &edges[i].s, &edges[i].e, &edges[i].w);<br />}<br />#endif<br />cal_mst(1, n, m);<br />lca_init(1, n);<br />st[1][0].first = 1;<br />st[1][0].second = 0;<br />for (int k = 1; (1<<k) <= n; ++k) for (int i = n; i >= 1; --i)<br />{<br />int ii = st[i][k-1].first;<br />st[i][k].first = st[ii][k-1].first;<br />st[i][k].second = max(st[i][k-1].second, st[ii][k-1].second);<br />}</p><p>int q;scanf("%d", &q);<br />while (q--)<br />{<br />int s, e;scanf("%d%d", &s, &e);<br />int lca = LCA(s, e);<br />printf("%d/n", max(query(s, lca), query(e, lca)));<br />}<br />}<br />return 0;<br />}