Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <string>
- #include <map>
- #include <set>
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- #include <cmath>
- #include <math.h>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <deque>
- #include <ctime>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int ui;
- #define mh() make_heap()
- #define poph() pop_heap()
- #define pushh() push_heap()
- #define sor(n) n.begin(), n.end()
- #define rsor(n) n.rbegin(), n.rend()
- #define mp make_pair
- #define files freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout)
- #define p(T) pair<T,T>
- #define znac(l) abs(l)/l
- const ll ok = ll(1e9 + 7);
- vector<p(ll)>graf[100000];
- p(ll)grafs[100000];
- void decsta(ll z)
- {
- grafs[z].first = 0;
- set < pair<int, int> > q;
- q.insert({ grafs[z].first, z });
- while (!q.empty()) {
- int v = q.begin()->second;
- q.erase(q.begin());
- for (size_t j = 0; j<graf[v].size(); ++j) {
- int to = graf[v][j].second,
- len = graf[v][j].first;
- if (grafs[v].first + len < grafs[to].first) {
- q.erase({ grafs[to].first, to });
- grafs[to].first = grafs[v].first + len;
- q.insert({ grafs[to].first, to });
- }
- }
- }
- }
- void decstb(ll z)
- {
- grafs[z].second = 0;
- set < pair<int, int> > q;
- q.insert({ grafs[z].second, z });
- while (!q.empty()) {
- int v = q.begin()->second;
- q.erase(q.begin());
- for (size_t j = 0; j<graf[v].size(); ++j) {
- int to = graf[v][j].second,
- len = graf[v][j].first;
- if (grafs[v].second+ len < grafs[to].second) {
- q.erase({ grafs[to].second, to });
- grafs[to].second = grafs[v].second + len;
- q.insert({ grafs[to].second, to });
- }
- }
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- files;
- #endif
- ll suma, sumb;
- cin >> suma >> sumb;
- ll n;
- cin >> n;
- for (int i = 0; i <= n + 2; i++)
- grafs[i] = { LLONG_MAX,LLONG_MAX };
- for (int i = 1; i <= n; i++)
- {
- ll a, b, sum;
- cin >> a >> b >> sum;
- graf[a].push_back({ sum ,b});
- graf[b].push_back({ sum,a });
- }
- ll a, b;
- cin >> a >> b;
- decsta(a);
- decstb(b);
- ll mn = LLONG_MAX;
- ll city = -1;
- vector<p(ll)> grafsg;
- for (int i = 1; i <= n; i++)
- {
- if (grafs[i].first <= suma&&grafs[i].second <= sumb&&grafs[i].first!=LLONG_MAX&&grafs[i].second != LLONG_MAX)
- {
- if (mn > grafs[i].first + grafs[i].second)
- mn = grafs[i].first + grafs[i].second;
- }
- }
- if (mn == LLONG_MAX)
- {
- cout << "Ajtow 4ever";
- return 0;
- }
- ll p = LLONG_MAX;
- for (int i = 1; i <= n; i++)
- {
- if (grafs[i].first + grafs[i].second == mn)
- {
- grafsg.push_back({ grafs[i].first - grafs[i].second,i });
- }
- }
- sort(sor(grafsg));
- for (int i = 0; i < grafsg.size(); i++)
- {
- if (grafs[grafsg[i].second].first <= suma&&grafs[grafsg[i].second].second <= sumb)
- {
- if (grafsg[i].first > 0)
- {
- city = grafsg[i].second;
- break;
- }
- city = grafsg[i].second;
- }
- }
- if (city==-1)
- {
- cout << "Ajtow 4ever";
- }
- else
- cout << city;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement