Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <iomanip>
- #include <queue>
- using namespace std;
- vector<bool> marks;
- // Обход в глубину
- void dfs(int current, const vector<vector<double>>& graph)
- {
- marks[current] = true;
- for(int i = 0; i < graph[current].size(); i++)
- {
- if(graph[current][i] != -1 && !marks[i])
- dfs(i, graph);
- }
- }
- // Обход в ширину
- void bfs(int start, const vector<vector<double>>& graph)
- {
- queue<int> q;
- q.push(start);
- while(!q.empty())
- {
- int curr = q.front();
- q.pop();
- marks[curr] = true;
- for(int i = 0; i < graph[curr].size(); i++)
- {
- if(!marks[graph[curr][i]])
- q.push(graph[curr][i]);
- }
- }
- }
- int main()
- {
- ifstream in("in.txt");
- ofstream out("out.txt");
- int i;
- in >> i;
- vector<vector<double>> matrix(i, vector<double>(i, 0));
- int a, b;
- double w;
- while (in >> a >> b >> w)
- {
- matrix[a-1][b-1] = w;
- }
- out << fixed << setprecision(1);
- for (int x = 0; x < matrix.size(); x++)
- {
- for (int y = 0; y < matrix.size(); y++)
- out << matrix[x][y] << '\t';
- out << endl;
- }
- marks.assign(i, false);
- dfs(0, matrix);
- for (int z = 0; z < marks.size(); z++)
- out << marks[z] << '\t';
- out << endl;
- // Нерабочий код
- // Для его работы нужно менять main()
- /*
- marks.assign(i, false);
- bfs(0, matrix);
- for (int z = 0; z < marks.size(); z++)
- out << marks[z] << '\t';
- out << endl;
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement