consolidate all repos to one for archive
This commit is contained in:
139
semester_2/osnove_algoritmov/naloga_5/main.cpp
Normal file
139
semester_2/osnove_algoritmov/naloga_5/main.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
void FLOYD_WARSHALL(int **&C, int **&D, int **&PI, int st_vozlisc) {
|
||||
|
||||
D = new int *[st_vozlisc];
|
||||
PI = new int *[st_vozlisc];
|
||||
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
PI[i] = new int[st_vozlisc];
|
||||
D[i] = new int[st_vozlisc];
|
||||
}
|
||||
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
for (int j = 0; j < st_vozlisc; j++) {
|
||||
D[i][j] = C[i][j];
|
||||
if (i != j && C[i][j] != INT16_MAX) { //inf
|
||||
PI[i][j] = i;
|
||||
} else {
|
||||
PI[i][j] = INT16_MAX; //nill
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int k = 0; k < st_vozlisc; k++) {
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
for (int j = 0; j < st_vozlisc; j++) {
|
||||
if (D[i][j] > D[i][k] + D[k][j]) {
|
||||
D[i][j] = D[i][k] + D[k][j];
|
||||
PI[i][j] = PI[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void beri(int **&C, int &st_vozlisc) {
|
||||
int st_povezav;
|
||||
std::ifstream f("graf3.txt");
|
||||
f >> st_vozlisc;
|
||||
f >> st_povezav;
|
||||
int v1, v2, cena;
|
||||
|
||||
C = new int *[st_vozlisc];
|
||||
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
C[i] = new int[st_vozlisc];
|
||||
}
|
||||
|
||||
for (int i = 0; i < st_vozlisc; i++) {
|
||||
for (int j = 0; j < st_vozlisc; j++) {
|
||||
if (i == j) {
|
||||
C[i][j] = 0;
|
||||
} else {
|
||||
C[i][j] = INT16_MAX; //inf
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < st_povezav; i++) {
|
||||
f >> v1 >> v2 >> cena;
|
||||
C[v1 - 1][v2 - 1] = cena;
|
||||
}
|
||||
std::cout << "Vozlisca: " << st_vozlisc << " Povezave: " << st_povezav << "\n";
|
||||
}
|
||||
|
||||
void izpisPoti(int **&PI, int s, int g) {
|
||||
if (s == g) {
|
||||
std::cout << s << " ";
|
||||
} else {
|
||||
if (PI[s][g] == INT16_MAX) { //inf
|
||||
std::cout << "med " << s << " in " << g << " ni poti";
|
||||
} else {
|
||||
izpisPoti(PI, s, PI[s][g]);
|
||||
std::cout << g << " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void menu() {
|
||||
std::cout << "Dijkstrov algoritem - izbira:\n"
|
||||
"1 Nalozi graf\n"
|
||||
"2 Zagon algoritma\n"
|
||||
"3 Izpis najkrajse poti\n"
|
||||
"0 Konec\n\n"
|
||||
"Vasa izbira: ";
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
int **C;
|
||||
int **D;
|
||||
int **PI;
|
||||
int st_vozlisc = 0;
|
||||
bool runing = true;
|
||||
int condition;
|
||||
int s, g;
|
||||
bool func = false;
|
||||
while (runing) {
|
||||
menu();
|
||||
std::cin >> condition;
|
||||
switch (condition) {
|
||||
case 1:
|
||||
beri(C, st_vozlisc);
|
||||
break;
|
||||
case 2:
|
||||
FLOYD_WARSHALL(C, D, PI, st_vozlisc);
|
||||
func = true;
|
||||
break;
|
||||
case 3:
|
||||
if (func) {
|
||||
do {
|
||||
std::cout << "Izberi s:";
|
||||
std::cin >> s;
|
||||
} while (s > st_vozlisc);
|
||||
do {
|
||||
std::cout << "Izberi g:";
|
||||
std::cin >> g;
|
||||
}while(g > st_vozlisc);
|
||||
std::cout << "Pot: ";
|
||||
izpisPoti(PI, s, g);
|
||||
std::cout << "\n Cena: " << D[s][g];
|
||||
std::cout << "\n";
|
||||
}
|
||||
break;
|
||||
case 0:
|
||||
runing = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user