Advertisement
Guest User

Untitled

a guest
Jun 20th, 2012
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. int velikostMatrike = 0;
  9. int C[15][15];
  10.  
  11. struct Pot{
  12.     vector <int> vozlisca;
  13.     int dolzina;
  14. };
  15.  
  16. queue<Pot> Q;
  17.  
  18. void Beri(int G[15][15], string file) {
  19.     fstream dat(file.c_str(), fstream::in);
  20.     dat >> velikostMatrike;
  21.     int p, q, c;
  22.  
  23.     for(int i=0;i<velikostMatrike;i++){
  24.         for(int j=0;j<velikostMatrike;j++){
  25.             C[i][j]=9999;
  26.         }
  27.     }
  28.     while (!dat.eof()) {
  29.         dat >> p >> q >> c;
  30.         G[p - 1][q - 1] = c;
  31.         C[q - 1][p - 1] = c;
  32.     }
  33.     dat.close();
  34. }
  35.  
  36. void VstaviVvrsto(queue<Pot> &Q, Pot pot){
  37.     Q.push(pot);
  38. }
  39.  
  40. Pot VzemiIzVrste(queue<Pot> &Q){
  41.     Pot pot = Q.front();
  42.     Q.pop();
  43.     return pot;
  44. }
  45.  
  46. Pot DodajVozlisce(Pot &pot, int v)
  47. {
  48.     pot.vozlisca.push_back(v);
  49.     return pot;
  50. }
  51.  
  52. bool Eksistira(int element, vector<int>vozlisca)
  53. {
  54.     for(unsigned int index=0; index<vozlisca.size(); index++)
  55.         if(element == vozlisca[index])
  56.             return true;
  57.     return false;
  58. }
  59.  
  60. void VstaviNovoResitev(queue<Pot> &pot, Pot p1){
  61.     for(int index = 0; index < pot.size(); index++)
  62.         pot.pop();
  63.     pot.push(p1);
  64. }
  65.  
  66. void DodajResitev(queue<Pot> &pot, Pot p1){
  67.     pot.push(p1);
  68. }
  69.  
  70. void Izpis(queue<Pot> R, int cenaResitve){
  71.     for(int i = 0; i < R.size(); i++){
  72.         cout << R.front().vozlisca[0] << endl;
  73.         cout << R.front().vozlisca[1] << endl;
  74.         cout << R.front().dolzina << endl;
  75.     }
  76.     cout << "R: " << R.front().vozlisca[0] << " in " << R.front().vozlisca[1] << " cena: " << cenaResitve << endl;
  77. }
  78.  
  79. void RazvejiInOmeji(int G[15][15], int s, int g, queue<Pot> &R, int &cenaResitve){
  80.     Pot pot;
  81.     pot.vozlisca.push_back(s);
  82.     pot.dolzina = 0;
  83.     cenaResitve = 9999;
  84.  
  85.     VstaviVvrsto(Q, pot);
  86.  
  87.     while(!Q.empty()){
  88.         Pot p = VzemiIzVrste(Q);
  89.         int u = p.vozlisca.back();
  90.         for(int v = 0; v < velikostMatrike; v++){
  91.             if(!Eksistira(v, p.vozlisca)){
  92.                 Pot p1 = DodajVozlisce(p, v);
  93.                 p1.dolzina = p.dolzina + C[u][v];
  94.                 if(v == g){
  95.                     if(p1.dolzina < cenaResitve){
  96.                         cenaResitve = p1.dolzina;
  97.                         VstaviNovoResitev(R, p1);
  98.                     }
  99.                     else if(p1.dolzina == cenaResitve){
  100.                         cenaResitve = p1.dolzina;
  101.                         DodajResitev(R, p1);
  102.                     }
  103.                 }
  104.                 else if(p.dolzina < cenaResitve){
  105.                         cenaResitve = p1.dolzina;
  106.                     VstaviVvrsto(Q, p1);
  107.                 }
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113. int main(){
  114.     int G[15][15];
  115.     queue<Pot> R;
  116.     int s, g, cenaResitve = 0;
  117.  
  118.     //RazvejiInOmeji(G, s, g, R, cenaResitve);
  119.     int izbira = 0;
  120.     do{
  121.         cout << "Razveji in omeji - izbira" << endl;
  122.         cout << "1 Preberi podatke iz datoteke" << endl;
  123.         cout << "2 Iskanje poti med vozliscema s in q" << endl;
  124.         cout << "3 Konec" << endl;
  125.         cout << "\r\nVasa izbira: ";
  126.         cin >> izbira;
  127.         switch(izbira){
  128.         case 1: Beri(G, "graf.txt"); break;
  129.         case 2: {
  130.                     cout << "Vpisi s: ";
  131.                     cin >> s;
  132.                     cout << "Vpisi g: ";
  133.                     cin >> g;
  134.                     if(s <= 0 || g <= 0 || s > velikostMatrike || g > velikostMatrike)
  135.                         cout << "Neveljaven vnos!" << endl;
  136.                     else{
  137.                         RazvejiInOmeji(G, s, g, R, cenaResitve);
  138.                         Izpis(R, cenaResitve);
  139.                     }
  140.                    
  141.                 }break;
  142.         case 3: break;
  143.         default: break;
  144.         }
  145.     }
  146.     while(izbira != 3);
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement