Advertisement
TwITe

Untitled

Jan 8th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. using ull = unsigned long long;
  5. const int MAXN = 1e5 + 1;
  6.  
  7. struct d {
  8.       ll s = -2;
  9. };
  10.  
  11. vector<unordered_set<int>> v;
  12. unordered_map <int, unordered_map<int, d>> cost;
  13.  
  14. void solve() {
  15.     ios::sync_with_stdio(false);
  16.     cin.tie(NULL);
  17.     int n, m;
  18.     cin >> n >> m;
  19.     v.resize(n + 1);
  20.     for (int i = 0; i < m; i++) {
  21.         int a, b;
  22.         ll c;
  23.         cin >> a >> b >> c;
  24.         v[a].insert(b);
  25.         v[b].insert(a);
  26.         cost[a][b].s = c;
  27.         cost[b][a].s = c;
  28.     }
  29.     int q;
  30.     cin >> q;
  31.  
  32.     for (int j = 0; j < q; j++) {
  33.         int key;
  34.         cin >> key;
  35.  
  36.         if (key == 1) {
  37.             int a, b;
  38.             ll c;
  39.             cin >> a >> b >> c;
  40.             v[a].insert(b);
  41.             v[b].insert(a);
  42.             cost[a][b].s = c;
  43.             cost[b][a].s = c;
  44.         }
  45.  
  46.         if (key == 2) {
  47.             int a, b;
  48.             cin >> a >> b;
  49.             cost[a][b].s = -2;
  50.             cost[b][a].s = -2;
  51.         }
  52.  
  53.         if (key == 3) {
  54.             int a, b;
  55.             cin >> a >> b;
  56.             vector<ll> w;
  57.  
  58.             ll ans = LONG_LONG_MAX;
  59.  
  60.             for (auto i : v[a]) {
  61.                 if (i == b) {
  62.                     if (cost[a][i].s != -2) {
  63.                         ans = min(ans, cost[a][i].s);
  64.                         w.emplace_back(cost[a][i].s);
  65.                     }
  66.                     continue;
  67.                 }
  68.                 else {
  69.                     if ((cost[a][i].s != -2 && cost[i][b].s != -2)) {
  70.                         ll sum = cost[a][i].s + cost[i][b].s;
  71.                         ans = min(ans, sum);
  72.                         w.emplace_back(sum);
  73.                     }
  74.                 }
  75.             }
  76.  
  77.             if (w.empty()) {
  78.                 ans = -1;
  79.             }
  80.  
  81.             cout << ans << endl;
  82.         }
  83.     }
  84. }
  85.  
  86. int main() {
  87.     solve();
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement