Advertisement
Guest User

Untitled

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