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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e+5 + 5;
struct krus { int frm, to; ll v; bool operator < (const krus &rhs) const { return v < rhs.v; } }k[maxn + 100]; int n, m, uf[maxn];
int tmp[100], tmps[100], tote, totp;
inline void init() { for(int i = 1; i <= n; ++i) uf[i] = i; } int find(int x) { return (uf[x] == x) ? x : uf[x] = find(uf[x]); } bool link(int x, int y) { int fx = find(x), fy = find(y); if(fx != fy) { uf[fx] = fy; return true; } return false; }
struct edge { int to, nxt; ll v; }e[maxn << 1]; int ptr, lnk[maxn], dep[maxn], f[maxn], top[maxn], mxson[maxn], siz[maxn]; ll ddis[maxn]; inline void add(int bgn, int end, ll val) { e[++ptr] = (edge){end, lnk[bgn], val}; lnk[bgn] = ptr; } void dfs(int x, int fa, int d) { siz[x] = 1; f[x] = fa; dep[x] = d; for(int p = lnk[x]; p; p = e[p].nxt) { int y = e[p].to; if(y == fa) continue; ddis[y] = ddis[x] + e[p].v; dfs(y, x, d + 1); siz[x] += siz[y]; if(siz[y] > mxson[x]) mxson[x] = y; } } void dfs2(int x, int init) { top[x] = init; if(!mxson[x]) return; dfs2(mxson[x], init); for(int p = lnk[x]; p; p = e[p].nxt) { int y = e[p].to; if(y == f[x] || y == mxson[x]) continue; dfs2(y, y); } } inline int lca(int x, int y) { while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); x = f[top[x]]; } return (dep[x] > dep[y]) ? y : x; }
int vis[maxn]; ll dis[50][maxn]; queue<int> q; inline void spfa(int i, int s) { memset(dis[i], 0x3f, sizeof dis[i]); q.push(s); dis[i][s] = 0; vis[s] = 1; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int p = lnk[u]; p; p = e[p].nxt) { int y = e[p].to; if(dis[i][y] > dis[i][u] + e[p].v) { dis[i][y] = dis[i][u] + e[p].v; if(!vis[y]) vis[y] = 1, q.push(y); } } } }
int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;init(); for(int i = 1; i <= m; ++i) cin >> k[i].frm >> k[i].to >> k[i].v; sort(k + 1, k + 1 + m); for(int i = 1; i <= m; ++i) { if(link(k[i].frm, k[i].to)) { add(k[i].frm, k[i].to, k[i].v); add(k[i].to, k[i].frm, k[i].v); } else tmp[++tote] = i; } dfs(1, 0, 1); dfs2(1, 1); for(int i = 1; i <= tote; ++i) { add(k[tmp[i]].frm, k[tmp[i]].to, k[tmp[i]].v); add(k[tmp[i]].to, k[tmp[i]].frm, k[tmp[i]].v); tmps[++totp] = k[tmp[i]].frm, tmps[++totp] = k[tmp[i]].to; } for(int i = 1; i <= totp; ++i) spfa(i, tmps[i]);
int q; cin >> q; while(q--) { int x, y; cin >> x >> y; int LCA = lca(x, y); ll ans = ddis[x] + ddis[y] - 2ll * ddis[LCA]; for(int i = 1; i <= totp; ++i) ans = min(ans, dis[i][x] + dis[i][y]); cout << ans << endl; }
return 0; }
|