Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin( "modernizare.in" );
  4. ofstream fout( "modernizare.out" );
  5.  
  6. #define all(x) (x).begin() , (x).end()
  7. #define pb push_back
  8. #define mp make_pair
  9. #define st first
  10. #define nd second
  11.  
  12. struct strada
  13. {
  14.     int v, x, i;
  15.     strada()
  16.     {
  17.         v = 0;
  18.         x = 0;
  19.         i = 0;
  20.     }
  21.     strada( int a, int b, int c )
  22.     {
  23.         x = a;
  24.         v = b;
  25.         i = c;
  26.     }
  27. };
  28.  
  29. int n, m, f;
  30. int use[100005];
  31. int usestr[100005];
  32.  
  33. vector<strada>nod[100005];
  34. vector < pair<int, pair<int, int> > >strazi[100005];
  35.  
  36. void bfs( int x )
  37. {
  38.     queue<strada>q;
  39.     q.push( strada( x, 0, 0 ) );
  40.     use[x] = 1;
  41.  
  42.     while( !q.empty() )
  43.         {
  44.             strada t = q.front();
  45.             q.pop();
  46.             int y = t.x;
  47.  
  48.             for( auto it : nod[y] )
  49.                 if( !use[it.x] )
  50.                     {
  51.                         use[it.x] = use[y] + 1;
  52.                         q.push( strada( it.x, 0, 0 ) );
  53.                     }
  54.         }
  55. }
  56. void str( int x )
  57. {
  58.     queue<int>q;
  59.     q.push( x );
  60.  
  61.     while( !q.empty() )
  62.         {
  63.             int y = q.front();
  64.             q.pop();
  65.  
  66.             for( auto it : nod[y] )
  67.                 if( !usestr[it.i] )
  68.                     {
  69.                         usestr[it.i] = min( use[y], use[it.x] );
  70.                         strazi[usestr[it.i]].pb( mp( min( use[y], use[it.x] ), mp( max( use[y], use[it.x] ), it.v ) ) );
  71.  
  72.                         q.push( it.x );
  73.                     }
  74.         }
  75. }
  76. int modernizare( int x )
  77. {
  78.     if( f == 0 || strazi[x].size() == 0 )
  79.         return 0;
  80.  
  81.     sort( all( strazi[x] ) );
  82.     int nr = 0;
  83.  
  84.     for( auto it : strazi[x] )
  85.         if( f - it.nd.nd >= 0 )
  86.             {
  87.                 nr++;
  88.                 f -= it.nd.nd;
  89.             }
  90.         else
  91.             break;
  92.  
  93.     if( nr == strazi[x].size() )
  94.         return nr + modernizare( x + 1 );
  95.     else
  96.         return nr;
  97. }
  98. int main()
  99. {
  100.     fin >> n >> m >> f;
  101.  
  102.     for( int i = 1; i <= m; i++ )
  103.         {
  104.             int x, y, v;
  105.             fin >> x >> y >> v;
  106.             nod[y].pb( strada( x, v, i ) );
  107.             nod[x].pb( strada( y, v, i ) );
  108.         }
  109.  
  110.     bfs( 1 );
  111.     str( 1 );
  112.     fout << modernizare( 1 );
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement