Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 1,788 Bytes
f7ba5f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <cmath>
#include <iostream>
#include <unordered_map>
using namespace std;

const int LIM = 200000;

int N, M, Q;
unordered_map<int, int> adj[LIM];

// big_ind_flow[u][v] = the total flow from u to v through length-2 paths
// if u is a "big" node with degree > sqrt(M), otherwise big_ind_flow[u] = {}.
unordered_map<int, long long> big_ind_flow[LIM];

inline int degree(int i) {
  return adj[i].size();
}

inline bool is_big(int i) {
  return degree(i) > (int)sqrt(M);
}

void solve() {
  for (int i = 0; i < LIM; i++) {
    adj[i].clear();
    big_ind_flow[i].clear();
  }
  cin >> N >> M >> Q;
  for (int i = 0, a, b, c; i < M; i++) {
    cin >> a >> b >> c;
    a--, b--;
    adj[a][b] = adj[b][a] = c;
  }
  // O(M^1.5) - precompute length-2 max flows for "big" nodes (deg > sqrt(M)).
  for (int u = 0; u < N; u++) {
    if (is_big(u)) {
      for (auto const &[mid, c1] : adj[u]) {
        for (auto const &[v, c2] : adj[mid]) {
          if (u != v) {
            big_ind_flow[u][v] += min(c1, c2);
          }
        }
      }
    }
  }
  // Answer queries.
  for (int i = 0, x, y; i < Q; i++) {
    cin >> x >> y;
    x--, y--;
    if (degree(x) < degree(y)) {
      swap(x, y);  // Always process queries from bigger node.
    }
    long long ans = 0;
    if (adj[x].count(y)) {
      ans += 2*adj[x][y];  // Fly direct x -> y.
    }
    if (is_big(x)) {
      ans += big_ind_flow[x][y];
    } else {
      for (auto const &[mid, c1] : adj[x]) {  // O(sqrt(M))
        if (adj[mid].count(y)) {
          ans += min(c1, adj[mid][y]);  // Fly indirect x -> mid -> y.
        }
      }
    }
    cout << " " << ans;
  }
}

int main() {
  int T;
  cin >> T;
  for (int t = 1; t <= T; t++) {
    cout << "Case #" << t << ":";
    solve();
    cout << endl;
  }
  return 0;
}