Advertisement
artemgf

Хрень от Лёши

Feb 26th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <set>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stdio.h>
  9. #include <cmath>
  10. #include <math.h>
  11. #include <queue>
  12. #include <stack>
  13. #include <climits>
  14. #include <deque>
  15. #include <ctime>
  16.  
  17. using namespace std;
  18.  
  19. typedef long long ll;
  20. typedef unsigned long long ull;
  21. typedef unsigned int ui;
  22.  
  23. #define mh() make_heap()
  24. #define poph() pop_heap()
  25. #define pushh() push_heap()
  26. #define sor(n) n.begin(), n.end()
  27. #define rsor(n) n.rbegin(), n.rend()
  28. #define mp make_pair
  29. #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
  30. #define p(T) pair<T,T>
  31. #define znac(l) abs(l)/l
  32. const ll ok = ll(1e9 + 7);
  33.  
  34. vector<p(ll)>graf[100000];
  35. p(ll)grafs[100000];
  36.  
  37. void decsta(ll z)
  38. {
  39.     grafs[z].first = 0;
  40.     set < pair<int, int> > q;
  41.     q.insert({ grafs[z].first, z });
  42.     while (!q.empty()) {
  43.         int v = q.begin()->second;
  44.         q.erase(q.begin());
  45.  
  46.         for (size_t j = 0; j<graf[v].size(); ++j) {
  47.             int to = graf[v][j].second,
  48.                 len = graf[v][j].first;
  49.             if (grafs[v].first + len < grafs[to].first) {
  50.                 q.erase({ grafs[to].first, to });
  51.                 grafs[to].first = grafs[v].first + len;
  52.                 q.insert({ grafs[to].first, to });
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. void decstb(ll z)
  59. {
  60.     grafs[z].second = 0;
  61.     set < pair<int, int> > q;
  62.     q.insert({ grafs[z].second, z });
  63.     while (!q.empty()) {
  64.         int v = q.begin()->second;
  65.         q.erase(q.begin());
  66.  
  67.         for (size_t j = 0; j<graf[v].size(); ++j) {
  68.             int to = graf[v][j].second,
  69.                 len = graf[v][j].first;
  70.             if (grafs[v].second+ len < grafs[to].second) {
  71.                 q.erase({ grafs[to].second, to });
  72.                 grafs[to].second = grafs[v].second + len;
  73.                 q.insert({ grafs[to].second, to });
  74.             }
  75.         }
  76.     }
  77. }
  78. int main()
  79. {
  80. #ifndef ONLINE_JUDGE
  81.     files;
  82. #endif
  83.     ll suma, sumb;
  84.     cin >> suma >> sumb;
  85.     ll n;
  86.     cin >> n;
  87.     for (int i = 0; i <= n + 2; i++)
  88.         grafs[i] = { LLONG_MAX,LLONG_MAX };
  89.     for (int i = 1; i <= n; i++)
  90.     {
  91.         ll a, b, sum;
  92.         cin >> a >> b >> sum;
  93.         graf[a].push_back({ sum ,b});
  94.         graf[b].push_back({ sum,a });
  95.     }
  96.     ll a, b;
  97.     cin >> a >> b;
  98.     decsta(a);
  99.     decstb(b);
  100.     ll mn = LLONG_MAX;
  101.     ll city = -1;
  102.     vector<p(ll)> grafsg;
  103.     for (int i = 1; i <= n; i++)
  104.     {
  105.         if (grafs[i].first <= suma&&grafs[i].second <= sumb&&grafs[i].first!=LLONG_MAX&&grafs[i].second != LLONG_MAX)
  106.         {
  107.             if (mn > grafs[i].first + grafs[i].second)
  108.                 mn = grafs[i].first + grafs[i].second;
  109.         }
  110.     }
  111.     if (mn == LLONG_MAX)
  112.     {
  113.         cout << "Ajtow 4ever";
  114.         return 0;
  115.     }
  116.     ll p = LLONG_MAX;
  117.     for (int i = 1; i <= n; i++)
  118.     {
  119.         if (grafs[i].first + grafs[i].second == mn)
  120.         {
  121.             grafsg.push_back({ grafs[i].first - grafs[i].second,i });
  122.         }
  123.     }
  124.     sort(sor(grafsg));
  125.     for (int i = 0; i < grafsg.size(); i++)
  126.     {
  127.         if (grafs[grafsg[i].second].first <= suma&&grafs[grafsg[i].second].second <= sumb)
  128.         {
  129.             if (grafsg[i].first > 0)
  130.             {
  131.                 city = grafsg[i].second;
  132.                 break;
  133.             }
  134.             city = grafsg[i].second;
  135.         }
  136.     }
  137.     if (city==-1)
  138.     {
  139.         cout << "Ajtow 4ever";
  140.     }
  141.     else
  142.         cout << city;
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement