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
| #include <bits/stdc++.h> #define int long long using namespace std;
const int G = 256;
int n, m, K; int dist[G][G], ans[G][G], c[G];
struct yhzhyhm { int val, id; bool operator < (const yhzhyhm &x) const { return val < x.val; } }yh[G];
inline void FLOYD() {
sort(yh + 1, yh + 1 + n); int k, i, j; for (k = 1; k <= n; ++ k) { for (i = 1; i <= n; ++ i) { for (j = 1; j <= n; ++ j) { dist[i][j] = min(dist[i][j], dist[i][yh[k].id] + dist[yh[k].id][j]); ans[i][j] = min(ans[i][j], dist[i][j] + max(yh[k].val, max(c[i], c[j]))); } } } return; }
signed main() { int i, x, y, z; n = read(), m = read(), K = read(); memset(ans, 0x3f, sizeof ans), memset(dist, 0x3f, sizeof dist); for (i = 1; i <= n; ++ i) { yh[i].val = read(); c[i] = yh[i].val, yh[i].id = i; ans[i][i] = yh[i].val, dist[i][i] = 0; }
for (i = 1; i <= m; ++ i) { x = read(), y = read(), z = read(); dist[x][y] = dist[y][x] = min(dist[x][y], z); } FLOYD(); for (i = 1; i <= K; ++ i) { int s = read(), t = read(); put(ans[s][t]); } return 0; }
|