Advertisement
a53

Modernizare (of):

a53
Feb 19th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NMAX 100005
  3. #define pii pair<int,int>
  4. using namespace std;
  5.  
  6. ifstream f("modernizare.in");
  7. ofstream g("modernizare.out");
  8.  
  9. int n, m, F, x, y, c, lg[NMAX];
  10. vector<int> v[NMAX];
  11. queue<int> Q;
  12. set<pii> S;
  13.  
  14. struct muchie{
  15. int x,y,c;
  16. };
  17. vector<muchie> M;
  18.  
  19. bool comp(muchie a, muchie b){
  20. if (min(lg[a.x],lg[a.y]) == min(lg[b.x],lg[b.y])){
  21. if (max(lg[a.x],lg[a.y]) == max(lg[b.x],lg[b.y]))
  22. return a.c < b.c;
  23. return max(lg[a.x],lg[a.y]) < max(lg[b.x],lg[b.y]);
  24. }
  25. return min(lg[a.x],lg[a.y]) < min(lg[b.x],lg[b.y]);
  26. }
  27.  
  28. int main()
  29. {
  30. f >> n >> m >> F;
  31. assert(1 <= n && n <= 1e5);
  32. assert(1 <= m && m <= 1e5);
  33. assert(1 <= F && F <= 2e9);
  34.  
  35. for (int i=1;i<=m;i++){
  36. f >> x >> y >> c;
  37. assert(1 <= x && x <= n);
  38. assert(1 <= y && y <= n);
  39. assert(1 <= c && c <= 2e9);
  40. assert(S.find({x,y}) == S.end());
  41. assert(x != y);
  42. S.insert({x,y}); S.insert({y,x});
  43.  
  44. v[x].push_back(y);
  45. v[y].push_back(x);
  46. M.push_back({x,y,c});
  47. }
  48.  
  49. Q.push(1);
  50. memset(lg,0x3f,sizeof(lg));
  51. lg[1] = 1;
  52. while (!Q.empty()){
  53. int nod = Q.front();
  54. Q.pop();
  55. for (auto it : v[nod]){
  56. if (lg[it] > 1e9){
  57. lg[it] = lg[nod] + 1;
  58. Q.push(it);
  59. }
  60. }
  61. }
  62.  
  63. sort(M.begin(),M.end(),comp);
  64.  
  65. int k = 0;
  66. for (int i=0;i < M.size() && lg[M[i].x] < 1e9 && M[i].c <= F;i++,k++)
  67. F -= M[i].c;
  68.  
  69. g << k << '\n';
  70.  
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement