Advertisement
candale

Drum minim

Mar 15th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include<fstream>
  2. #include<queue>
  3. using namespace std ;
  4. ifstream fin("prog.in");
  5. ofstream fout("prog.out");
  6. queue <int> q;
  7. int a[100][100], n , x , y;
  8. void citire()
  9. {
  10. int i , j;
  11. fin>>n>>x>>y;
  12. while(fin>>i>>j)
  13. a[i][j]=a[j][i]=1;
  14. fin.close();
  15. }
  16. void BF(int nd,int sel[], int t[])
  17. {
  18. int i , j;
  19. sel[nd]=1;
  20. q.push(nd);
  21. while(!q.empty())
  22. {
  23. i=q.front();
  24. for( j =1;j<=n;j++)
  25. if(a[i][j]==1 && sel[j]==0)
  26. {
  27. sel[j]=1;
  28. t[j]=i;
  29. q.push(j);
  30. }
  31. q.pop();
  32. }
  33. }
  34. void drum(int final, int t[])
  35. {
  36. if (t[final]!=0)
  37. {
  38. drum(t[final],t);
  39. fout<<final<<' ';
  40. }
  41. }
  42. int main()
  43. {
  44. citire();
  45. int t[100];
  46. int sel[100];
  47. for(int i=1;i<=n;i++)
  48. sel[i]=t[i]=0;
  49. BF(x,sel,t);
  50. for(int i=1;i<=7;i++)
  51. fout<<t[i]<<' ';
  52. if(t[y]!=0)
  53. drum(y,t);
  54. else
  55. fout<<"Nu exista drum"<<endl;
  56. fout.close();
  57. return 0;
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement