Advertisement
GUBILIN

Untitled

Feb 17th, 2020
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. float c[100][100],infinite=10000;
  4. int n,m,dr[100][100];
  5. int sume[101];
  6. ifstream f("2.in");
  7. int x,y,d;
  8. void citire()
  9. {
  10. int i,j;
  11. f>>n>>m;
  12. for(i=1;i<=m;i++)
  13. {
  14. f>>x>>y>>d;
  15. c[x][y]=d;
  16. }
  17. for(i=1;i<=n;i++)
  18. for(j=1;j<=n;j++)
  19. if(i!=j && c[i][j]==0)
  20. c[i][j]=infinite;
  21. }
  22.  
  23. void drumul_initial()
  24. {
  25. int i,j;
  26. for(i=1;i<=n;i++)
  27. for(j=1;j<=n;j++)
  28. {
  29. if(i!=j&&c[i][j]!=infinite)
  30. dr[i][j]=i;
  31. if(i!=j&&c[i][j]==infinite)
  32. dr[i][j]=infinite;
  33. }}
  34.  
  35. void drum(int i,int j)
  36. {
  37. if(i==j)
  38. cout<<i<<" ";
  39. else {drum(i,dr[i][j]);
  40. cout<<j<<" ";}
  41.  
  42. }void afisare()
  43. {
  44. int i,j;
  45. for(i=1;i<=n;i++)
  46. for(j=1;j<=n;j++)
  47. if(i!=j)
  48. if(c[i][j]!=infinite)
  49. {
  50. cout<<"costul drumului de la "<<i<<" la "<<j<<" = "<<c[i][j];
  51. cout<<" drumul este: ";drum(i,j);
  52. cout<<endl;
  53. }
  54. }
  55. void Floyd()
  56. {
  57. int i,j,k;
  58. for(k=1;k<=n;k++)
  59. for(i=1;i<=n;i++)
  60. for(j=1;j<=n;j++)
  61. if(c[i][j]>c[i][k]+c[k][j])
  62. {
  63. c[i][j]=c[i][k]+c[k][j];
  64. dr[i][j]=dr[k][j];
  65. }
  66. }
  67. int main()
  68. {
  69. citire();
  70. drumul_initial();
  71. Floyd();
  72. int minim=10000;
  73. int i,j;
  74. int k=0;
  75. for(i=1;i<=n;i++)
  76. {int sumatotala=0;
  77. for(j=1;j<=n;j++)
  78. sumatotala+=dr[i][j];
  79. cout<<sumatotala<<" ";
  80. if(sumatotala<minim){sume[++k]=sumatotala;minim=sumatotala;}
  81. }
  82. int poz;
  83. for(i=1;i<=k;i++)
  84. {
  85. if(sume[i]==minim)poz=i;
  86. }
  87. cout<<"Nodul sursa este "<<i;
  88.  
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement