Sergey101

Q1

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