Sergey101

Q2

Jan 30th, 2025
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. typedef int_fast64_t lli;
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     ios_base::sync_with_stdio(false);
  8.     cin.tie(nullptr);
  9.     cout.tie(nullptr);
  10.  
  11.     lli a, n, m;
  12.     cin >> a >> n >> m;
  13.     map<string, map<string, lli> > mp1;
  14.     map<string, map<string, lli> > mp2;
  15.     for(lli i = 0; i < n; i++)
  16.     {
  17.         lli w;
  18.         string s1, s2;
  19.         cin >> s1 >> s2 >> w;
  20.         mp1[s1][s2] = w+1;
  21.     }
  22.     mp2 = mp1;
  23.     for(lli i = 0; i < m; i++)
  24.     {
  25.         lli w;
  26.         string s1, s2;
  27.         cin >> s1 >> s2 >> w;
  28.         mp2[s1][s2] = w+1;
  29.     }
  30.  
  31.     lli ans = 0;
  32.     for(auto x : mp1)
  33.     {
  34.         for(auto y : x.second)
  35.         {
  36.             lli cost = y.second - 1;
  37.             lli mn = 1e10;
  38.  
  39.             for(auto xx : mp2[x.first])
  40.             {
  41.                 if(xx.first == y.first)
  42.                 {
  43.                     mn = min(mn, xx.second - 1);
  44.                 }
  45.                 else if(mp2[xx.first][y.first] != 0)
  46.                 {
  47.                     mn = min(mn, xx.second + mp2[xx.first][y.first] - 2);
  48.                 }
  49.             }
  50.  
  51.             //cout << mn << endl;
  52.             ans += cost-mn;
  53.         }
  54.     }
  55.  
  56.     cout << ans << '\n';
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment