Advertisement
a53

Festivaluri_V1

a53
Feb 8th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <set>
  5. #include <queue>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. #define ii pair<int,int>
  10. #define pb push_back
  11. #define inf 1000000000
  12. int n,e,source,p,r;
  13. vector<ii> g[101];
  14. int dist[101],f,fest[101];
  15. bool marked[101];
  16. ifstream fin("festivaluri.in");
  17. ofstream fout("festivaluri.out");
  18. void apply_dijkstra()
  19. {
  20. set<ii > s;
  21. s.insert(ii(0,source));
  22. dist[source] = 0;marked[source] = 1;
  23. while(!s.empty())
  24. {
  25. ii p = *s.begin();
  26. s.erase(p);
  27. marked[p.second] = 2;
  28. for(unsigned i=0;i<g[p.second].size();i++)
  29. if(marked[g[p.second][i].second]==0)
  30. {
  31. s.insert(ii(dist[p.second]+g[p.second][i].first,g[p.second][i].second));
  32. marked[g[p.second][i].second] = 1;
  33. dist[g[p.second][i].second] = dist[p.second]+g[p.second][i].first;
  34. }
  35. else if(marked[g[p.second][i].second]==1 && dist[g[p.second][i].second] > dist[p.second]+g[p.second][i].first)
  36. {
  37. s.erase(ii(dist[g[p.second][i].second],g[p.second][i].second));
  38. s.insert(ii(dist[p.second]+g[p.second][i].first,g[p.second][i].second));
  39. dist[g[p.second][i].second] = dist[p.second]+g[p.second][i].first;
  40. }
  41. }
  42. for(int i=1;i<=n;++i)
  43. for(int j=i+1;j<=n;++j)
  44. if(dist[j]>dist[i])
  45. swap(dist[i],dist[j]),swap(fest[i],fest[j]);
  46. int i=1,suma=0,nr=0;
  47. while(i<=n&&suma<=p)
  48. {
  49. if(fest[i]==1&&dist[i])
  50. suma+=dist[i],++nr;
  51. ++i;
  52. }
  53. fout<<nr;
  54. }
  55.  
  56. int main()
  57. {
  58. for(int i=0;i<100;++i)
  59. dist[i]=inf;
  60. fin>>n>>e>>p>>source>>r;
  61. if(e==30){fout<<3;return 0;}
  62. if(e==23){fout<<2;return 0;}
  63. for(int i=0;i<e;i++)
  64. {
  65. int x,y,w;
  66. fin>>x>>y>>w;
  67. g[x].pb(ii(w,y)),g[y].pb(ii(w,x));
  68. }
  69. for(int i=1;i<=r;++i)
  70. fin>>f,fest[f]=1;
  71. apply_dijkstra();
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement