145 lines
3.7 KiB
C++
145 lines
3.7 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <vector>
|
|
#include <string>
|
|
|
|
struct vozlisce {
|
|
int vez, predhodnik, cena;
|
|
};
|
|
|
|
int izlociNajmanjsega(std::vector<int> &voz, const std::vector<vozlisce> &rez) {
|
|
int tmp = voz[0];
|
|
int rem_voz = 0;
|
|
for (int i = 0; i < voz.size(); ++i) {
|
|
if (rez[tmp].cena > rez[voz[i]].cena) {
|
|
tmp = rez[voz[i]].vez;
|
|
rem_voz = i;
|
|
}
|
|
}
|
|
|
|
if(rem_voz == 0){
|
|
voz.erase(voz.begin());
|
|
}else {
|
|
voz.erase(voz.begin() + rem_voz);
|
|
}
|
|
return tmp;
|
|
}
|
|
|
|
void dijkstrovAlgoritem(int **&G, int s, const int st_vozlisc, std::vector<vozlisce> &rez) {
|
|
std::vector<int> Q;
|
|
for (int i = 0; i < st_vozlisc; ++i) {
|
|
vozlisce tmp;
|
|
tmp.vez = i;
|
|
tmp.predhodnik = -1;
|
|
tmp.cena = INT32_MAX;
|
|
Q.push_back(i);
|
|
rez.push_back(tmp);
|
|
}
|
|
|
|
rez[s].cena = 0;
|
|
int preg_voz = 0;
|
|
while (!Q.empty()) {
|
|
preg_voz = izlociNajmanjsega(Q, rez);
|
|
for (int i = 0; i < st_vozlisc; i++) {
|
|
if (G[preg_voz][i] > 0) {
|
|
if(rez[i].cena > rez[preg_voz].cena + G[preg_voz][i]){
|
|
rez[i].cena = rez[preg_voz].cena + G[preg_voz][i];
|
|
rez[i].predhodnik = preg_voz;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void izpisPoti(const std::vector<vozlisce> &rez, const int s, int v){
|
|
if(s == rez[v].vez){
|
|
std::cout << rez[v].vez << " cena: " << rez[v].cena << "\n";
|
|
return;
|
|
}else{
|
|
izpisPoti(rez,s,rez[v].predhodnik);
|
|
std::cout << rez[v].vez << " cena: " << rez[v].cena << "\n";
|
|
return;
|
|
}
|
|
}
|
|
|
|
void beri(int **&G, int &st_vozlisc) {
|
|
int st_povezav;
|
|
std::ifstream f("graf.txt");
|
|
f >> st_vozlisc;
|
|
f >> st_povezav;
|
|
int v1, v2, cena;
|
|
|
|
G = new int *[st_vozlisc];
|
|
|
|
for (int i = 0; i < st_vozlisc; i++) {
|
|
G[i] = new int[st_vozlisc];
|
|
}
|
|
|
|
for (int i = 0; i < st_vozlisc; i++) {
|
|
for (int j = 0; j < st_vozlisc; j++) {
|
|
G[i][j] = 0;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < st_povezav; i++) {
|
|
f >> v1 >> v2 >> cena;
|
|
G[v1 - 1][v2 - 1] = cena;
|
|
G[v2 - 1][v1 - 1] = cena;
|
|
}
|
|
std::cout << "Vozlisca: " << st_vozlisc << " Povezave: " << st_povezav << "\n";
|
|
|
|
// for (int i = 0; i < st_vozlisc; ++i) {
|
|
// for (int j = 0; j < st_vozlisc; ++j) {
|
|
// std::cout << G[j][i] << " ";
|
|
// }
|
|
// std::cout << "\n";
|
|
// }
|
|
|
|
}
|
|
|
|
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 **G;
|
|
int st_vozlisc = 0;
|
|
std::vector<vozlisce> rez;
|
|
|
|
bool runing = true;
|
|
int condition;
|
|
int tmp, iskanoVozlisce;
|
|
while(runing){
|
|
menu();
|
|
std::cin >> condition;
|
|
switch (condition) {
|
|
case 1:
|
|
beri(G, st_vozlisc);
|
|
break;
|
|
case 2:
|
|
std::cout << "Iskano vozlisce: ";
|
|
std::cin >> iskanoVozlisce;
|
|
dijkstrovAlgoritem(G, iskanoVozlisce, st_vozlisc, rez);
|
|
break;
|
|
case 3:
|
|
// for (int i = 0; i < rez.size(); ++i) {
|
|
// std::cout << rez[i].vez << " " << rez[i].predhodnik << " " << rez[i].cena << "\n";
|
|
// }
|
|
std::cout << "Izberi vozlisce: ";
|
|
std::cin >> tmp;
|
|
izpisPoti(rez,iskanoVozlisce, tmp);
|
|
break;
|
|
case 0:
|
|
runing = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
} |