Advertisement
Josif_tepe

Untitled

Jan 25th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5. const int maxn = 100005;
  6. vector<int> graph[maxn];
  7.  
  8. struct node {
  9.     int idx, passed_cities, quality;
  10.     node () {}
  11.     node(int _idx, int _passed_cities, int _quality) {
  12.         idx = _idx;
  13.         passed_cities = _passed_cities;
  14.         quality = _quality;
  15.     }
  16.     bool operator < (const node & tmp) const {
  17.         int score1 = 0, score2 = 0;
  18.         if(quality > tmp.quality) {
  19.             score1++;
  20.         }
  21.         else {
  22.             score2++;
  23.         }
  24.        
  25.         if(passed_cities < tmp.passed_cities) {
  26.             score1++;
  27.         }
  28.         else {
  29.             score2++;
  30.         }
  31.         return score1 < score2;
  32.     }
  33. };
  34. int main()
  35. {
  36.     int n, m, k;
  37.     cin >> n >> m >> k;
  38.    
  39.     vector<int> quality(n);
  40.     for(int i = 0; i < n; i++) {
  41.         cin >> quality[i];
  42.     }
  43.    
  44.     for(int i = 0; i < m; i++) {
  45.         int a, b;
  46.         cin >> a >> b;
  47.         a--;
  48.         b--;
  49.         graph[a].push_back(b);
  50.         graph[b].push_back(a);
  51.     }
  52.     priority_queue<node> pq;
  53.     pq.push(node(0, 1, quality[0]));
  54.     vector<bool> visited(n, false);
  55.     vector<int> dist(n, -2e9);
  56.     int res_quality = -2e9, res_passed_cities = 2e9;
  57.     while(!pq.empty()) {
  58.         node c = pq.top();
  59.         pq.pop();
  60.        
  61.         if(c.idx == n - 1) {
  62.             if(c.quality > res_quality) {
  63.                 res_quality = c.quality;
  64.                 res_passed_cities = c.passed_cities;
  65.             }
  66.             else if(c.quality == res_quality) {
  67.                 if(res_passed_cities > c.passed_cities) {
  68.                     res_passed_cities = c.passed_cities;
  69.                 }
  70.             }
  71.             continue;
  72.         }
  73.        
  74.         for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  75.             int neighbour = graph[c.idx][i];
  76.             if(c.passed_cities < k and dist[neighbour] < min(c.quality, quality[neighbour])) {
  77.                 dist[neighbour] = min(c.quality, quality[neighbour]);
  78.                 pq.push(node(neighbour, c.passed_cities + 1, dist[neighbour]));
  79.             }
  80.         }
  81.     }
  82.     cout << res_quality << " " << res_passed_cities << endl;
  83.        
  84.     return 0;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement