Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include<fstream>
  4. using namespace std;
  5. int main(){
  6. ifstream in;
  7. ofstream ou;
  8. in.open("input.txt");
  9. ou.open("output.txt");
  10. int nodi;
  11. in>>nodi;
  12. nodi--;
  13. int gallerie;
  14. in>>gallerie;
  15. int inizio,fine;
  16. in>>inizio;
  17. in>>fine;
  18. int visitato[nodi];
  19. int matrice[nodi][nodi];
  20. for(int i=0;i<nodi;i++){
  21. visitato[i]=0;
  22. for(int j=0;j<nodi;j++)
  23. matrice[i][j]=0;
  24. }
  25. int a,b;
  26. for(int i=0;i<gallerie;i++){
  27. in>>a;
  28. in>>b;
  29. a--;
  30. b--;
  31. in>>matrice[a][b];
  32. }
  33. inizio--;
  34. fine--;
  35. int cont=0;
  36. int s=inizio;
  37. bool t=true;
  38. pair<int,int> fro;
  39. while(t){
  40. t=false;
  41. fro.first=50000;
  42. fro.second=0;
  43. if(visitato[s]==0){
  44. visitato[s]=1;
  45. for (int i=0;i<nodi;i++){
  46. if(matrice[s][i]<fro.first&&matrice[s][i]!=0){
  47. fro.first=matrice[s][i];
  48. fro.second=i;
  49. t=true;
  50. }
  51. }
  52. }else{
  53. t=false;
  54. }
  55. if(t==true){
  56. s=fro.second;
  57. cont++;
  58. }
  59. if(t==false)
  60. cont=-1;
  61. }
  62. ou<<cont;
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement