Advertisement
Guest User

cf449b

a guest
Oct 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int MAXN = 300030;
  10. long long n, m, k, u, v, x, tr[MAXN], rj, d1, a, b;
  11. vector <pair <long long, long long> > graf[MAXN];
  12. vector <pair <long long, long long> > trn;
  13. priority_queue <pair <long long, long long> > q;
  14. bool bio[MAXN], utr[MAXN];
  15. long long d[MAXN];
  16.  
  17. void dijkstra () {
  18.     bio[1] = 1;
  19.     for (int i = 0; i < graf[1].size(); i++) {
  20.         q.push(make_pair(-graf[1][i].second, graf[1][i].first));
  21.     }
  22.     for (int i = 0; i < k; i++) {
  23.         d1 = trn[i].first;
  24.         if (!q.empty()) {
  25.             a = -q.top().first;
  26.             b = q.top().second;
  27.         }
  28.         //cout << "Vlak za " << trn[i].second << endl;
  29.         while (!q.empty() && a <= d1) {
  30.             //cout << a << ' ' << b <<  endl;
  31.             q.pop();
  32.             if (!bio[b]) {
  33.                 bio[b] = 1;
  34.                 d[b] = a;
  35.                 for (int j = 0; j < graf[b].size(); j++) {
  36.                     //cout << graf[b][j].second;
  37.                     //cout << "nema gr";
  38.                     if (!bio[graf[b][j].first]) {
  39.                         //cout << "nesto dodao";
  40.                         q.push(make_pair(-d[b] - graf[b][j].second, graf[b][j].first));
  41.                     }
  42.                 }
  43.                 //cout << "zavrsio for";
  44.             }
  45.             if (!q.empty()) {
  46.                 a = -q.top().first;
  47.                 b = q.top().second;
  48.             }
  49.         }
  50.         if (!bio[trn[i].second]) {
  51.             q.push(make_pair(-trn[i].first, trn[i].second));
  52.             //cout << "Iskoristio sam vlak " << trn[i].second << endl;
  53.             rj++;
  54.         }
  55.     }
  56. }
  57.  
  58. int main () {
  59.     cin >> n >> m >> k;
  60.     for (int i = 0; i < m; i++) {
  61.         scanf ("%I64d %I64d %I64d", &u, &v, &x);
  62.         graf[u].push_back(make_pair(v, x));
  63.         graf[v].push_back(make_pair(u, x));
  64.     }
  65.     for (int i = 0; i < k; i++) {
  66.         scanf ("%I64d %I64d", &u, &x);
  67.         trn.push_back(make_pair(x, u));
  68.     }
  69.     sort(trn.begin(), trn.end());
  70.     dijkstra();
  71.     cout << k - rj;
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement