fork download
  1. #include <iostream>
  2. using namespace std;
  3. #define infinity 999
  4. int numN;
  5. int cost[15][15];
  6. int pred[15];
  7. int d[15];
  8.  
  9. void init(int source){
  10. for(int i=0; i<numN; i++)
  11. d[i]=infinity, pred[i]=-1;
  12.  
  13. d[source]=0;
  14. }
  15.  
  16. void RELAX(int u, int v){
  17. if(d[v]>d[u]+cost[u][v])
  18. d[v]= d[u]+cost[u][v], pred[v]=u;
  19. }
  20. (
  21. void BELLMAN_FORD()
  22. {
  23.  
  24. }
  25. void print()
  26. {
  27. for(int i=0; i<numN; i++)
  28. { cout<<"\n";
  29. for(int j=0; j<numN; j++)
  30. cout<<cost[i][j]<<"\t";
  31. }
  32. }
  33.  
  34. int main() {
  35. int source, val;
  36. #if 0
  37. cout<<"enter the number of nodes"<<endl;
  38. cin>>numN;
  39. cout<<"enter the cost matrix of the graph"<<endl;
  40. for(int i=0; i<numN; i++)
  41. for(int j=0; j<numN; j++)
  42. { cin<<val;
  43. if(val==infinity)
  44. cost[i][j]= 0;
  45. else
  46. cost[i][j]=val;
  47. }
  48. cout<<"enter the start vertex"<<endl;
  49. cin>>source;
  50. #else
  51. source =0;
  52. numN=5;
  53. cost[0][0]=0;
  54. cost[0][1]=6;
  55. cost[0][2]=0;
  56. cost[0][3]=7;
  57. cost[0][4]=0;
  58.  
  59. cost[1][0]=0;
  60. cost[1][1]=0;
  61. cost[1][2]=5;
  62. cost[1][3]=8;
  63. cost[1][4]=-4;
  64.  
  65. cost[2][0]=0;
  66. cost[2][1]=-2;
  67. cost[2][2]=0;
  68. cost[2][3]=0;
  69. cost[2][4]=0;
  70.  
  71. cost[3][0]=0;
  72. cost[3][1]=0;
  73. cost[3][2]=-3;
  74. cost[3][3]=0;
  75. cost[3][4]=9;
  76.  
  77. cost[4][0]=2;
  78. cost[4][1]=0;
  79. cost[4][2]=7;
  80. cost[4][3]=0;
  81. cost[4][4]=0;
  82.  
  83. #endif
  84.  
  85. print();
  86. init(source);
  87. bellmanford();
  88. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
0	6	0	7	0	
0	0	5	8	-4	
0	-2	0	0	0	
0	0	-3	0	9	
2	0	7	0	0