Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("modernizare.in");
  6. ofstream fout("modernizare.out");
  7.  
  8. struct val{
  9.  
  10.     int nmin, nmax, cost;
  11. };
  12.  
  13. bool comp(val a, val b){
  14.  
  15.     if(a.nmin == b.nmin){
  16.  
  17.         if(a.nmax == b.nmax)
  18.  
  19.             return a.cost < b.cost;
  20.        
  21.         return a.nmax < b.nmax;
  22.     }
  23.  
  24.     return a.nmin < b.nmin;
  25. }
  26.  
  27. int main(){
  28.  
  29.     int n, m, fond; fin >> n >> m >> fond;
  30.  
  31.     vector<int> nod[n + 1];
  32.  
  33.     vector<val> muchie;
  34.  
  35.     int viz[n + 1];
  36.  
  37.     for(int i = 1; i <= m; i++){
  38.  
  39.         int x, y, c;    fin >> x >> y >> c;
  40.  
  41.         viz[x] = viz[y] = 0;
  42.  
  43.         nod[x].push_back(y); nod[y].push_back(x);
  44.  
  45.         muchie.push_back({x, y, c});
  46.     }
  47.  
  48.     int coada[n + 1], ultim = 0, prim = 1; coada[++ultim] = 1;  viz[1] = 1;
  49.  
  50.     while(prim <= ultim){
  51.  
  52.         int cur = coada[prim];
  53.  
  54.         for(auto i: nod[cur])
  55.  
  56.             if(viz[i] == 0){
  57.  
  58.                 coada[++ultim] = i;
  59.  
  60.                 viz[i] = viz[cur] + 1;
  61.             }
  62.  
  63.         prim++;
  64.     }
  65.  
  66.     for(auto i: muchie){
  67.  
  68.         i.nmin = min(viz[i.nmin], viz[i.nmax]);
  69.  
  70.         i.nmax = max(viz[i.nmin], viz[i.nmax]);
  71.     }
  72.  
  73.     sort(muchie.begin(), muchie.end(), comp);
  74.  
  75.     int s = 0;
  76.  
  77.     while(!muchie.empty() && fond - muchie[0].cost >= 0){
  78.  
  79.         s++;
  80.  
  81.         fond -= muchie[0].cost;
  82.  
  83.         muchie.erase(muchie.begin());
  84.     }
  85.  
  86.     fout << s;
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement