Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Macierz oraz lista sąsiedztwa grafu nieskierowanego
- #include<iostream>
- #include<fstream>
- #include<cstdlib>
- #include<iomanip> //umożliwia użycie funkcji setw()
- using namespace std;
- const int size = 30;
- int main()
- {
- cout << "Podaj nazwe pliku z ktorego zostana pobrane dane: " << endl;
- char filename[size];
- cin.getline(filename, size);
- ifstream inFile(filename);
- if (!inFile.is_open())
- {
- cout << "Otwarcie pliku " << filename << " nie powiodlo sie." << endl;
- cout << "Program zostanie zakonczony." << endl;
- exit(EXIT_FAILURE);
- }
- int n,max;
- inFile >> n;
- for (int i = 0; i < n; i++)
- {
- int a;
- inFile >> a;
- int b;
- inFile >> b;
- if (a>b)
- {
- max = a;
- }
- else max = b;
- }
- int **table_array = new int *[max]; //alokacja pamięci
- for (int i = 0; i < max; i++)
- {
- table_array[i] = new int[max];
- }
- //wypełnianie zerami macierzy
- for (int x = 0; x < max; x++)
- {
- for (int y = 0; y < max; y++)
- {
- table_array[x][y] = 0;
- }
- }
- inFile.close();
- inFile.open(filename);
- int m;
- inFile >> m;
- for (int i = 0; i < n; i++)
- {
- int a;
- inFile >> a;
- int b;
- inFile >> b;
- table_array[a - 1][b - 1] = 1;
- table_array[b - 1][a - 1] = 1;
- }
- for (int i = 0; i < max; i++)
- {
- cout << endl << setw(2);
- for (int j = 0; j < max; j++) cout << setw(2) << table_array[i][j];
- }
- cout << endl << endl;
- // Usuwamy macierz z pamięci komputera
- for (int i = 0; i < max; i++) delete[] table_array[i];
- delete[] table_array;
- inFile.close();
- inFile.open(filename);
- //tworzenie listy sasiedztwa
- int x;
- inFile >> x;
- for (int i = 0; i < x; i++)
- {
- int a;
- inFile >> a;
- int b;
- inFile >> b;
- int k;
- if (table_array[a][b] == 0) cout << "Drogi nie ma\n";
- else
- {
- cout << a << endl;
- k = a;
- while (k != b)
- {
- k = table_array[k][b];
- cout << k << endl;
- }
- }
- cout << endl;
- }
- inFile.close();
- cin.get();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement