Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Graph
- {
- private:
- int n, **arr, **vse_rebra, shet_reber, *ves, *path;
- public:
- Graph (int **matrix, int x, int shet, int *w)
- {
- n = x;
- vse_rebra = matrix;
- shet_reber = shet;
- ves = w;
- }
- void algorithm_ford();
- void get_result_ford();
- };
- void Graph::algorithm_ford()
- {
- path = new int[n];
- path[0] = 0;
- for (int i = 1; i < n; i++)
- path[i] = 1000;
- for (int i = 0; i < n-1; i++)
- for (int j = 0; j < shet_reber; j++)
- if (path[vse_rebra[j][0]] < 1000)
- if (path[vse_rebra[j][0]] + ves[j] < path[vse_rebra[j][1]])
- path[vse_rebra[j][1]] = path[vse_rebra[j][0]] + ves[j];
- }
- void Graph::get_result_ford()
- {
- for (int i = 0; i < n; i++)
- {
- if (path[i] == 1000)
- cout << "No" << " ";
- else
- cout << path[i] << " ";
- }
- }
- int main()
- {
- int n, shet_reber;
- cin >> n >> shet_reber;
- int **arr = new int*[shet_reber];
- int *w = new int[shet_reber];
- for ( int i = 0; i < shet_reber; i++)
- arr[i] = new int[2];
- for (int i = 0; i < shet_reber; i++)
- {
- for (int j = 0; j < 2; j++)
- {
- cin >> arr[i][j];
- arr[i][j]--;
- }
- cin >> w[i];
- }
- Graph object(arr, n, shet_reber, w);
- object.algorithm_ford();
- object.get_result_ford();
- }
Add Comment
Please, Sign In to add comment