Advertisement
nguyenvanquan7826

Dijkstra

Oct 12th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <queue>
  10. #include <stack>
  11. #define INP "input.INP"
  12. #define OUT "output.OUT"
  13. using namespace std;
  14.  
  15. typedef int item;
  16. typedef struct GRAPH
  17. {
  18.     char *name; // ten cac dinh
  19.     item **G;   // ma tran trong so
  20.     int n;      // so phan tu cua do thi
  21. } Graph;
  22.  
  23. void input_file();// lay du lieu tu file
  24. void input_B_E(int &a, int &b); //nhap vao dinh dau va cuoi
  25. void output(int a, int b);//Xuat ket qua tu file ra
  26. void Dijkstra(int a, int b);//thuat toan Dijkstra
  27. void Menu(int &select);
  28. item tongthiethai(); //tong quang duong di cua moi dinh (thay the cho vo cung trong ma tran trong so)
  29.  
  30. Graph Gr;
  31. int *Len;//Gia tri nho nhat tu a -> i. Len1 danh dau do dai.
  32. int *S;//Danh dau dinh thuoc danh sach dac biet
  33. int *P;//truy vet
  34.  
  35. int main()
  36. {
  37.     Graph Gr;
  38.     input_file();
  39.     int a, b, *P, i, select = 1;
  40.     while (select)
  41.     {
  42.         cout<<endl<<"-----Thuat toan Dijkstra-----"<<endl;
  43.         input_B_E(a, b);
  44.         Dijkstra(a, b);
  45.         output(a, b);
  46.         Menu(select);
  47.         if (!select) break;
  48.     }
  49.     return 0;
  50. }
  51.  
  52. void input_file()   // nhap ma tran ke tu file
  53. {
  54.     ifstream inp(INP);
  55.     if (inp == NULL)
  56.     {
  57.         cout<<"No found file input";
  58.         return;
  59.     }
  60.     inp >> Gr.n ;
  61.    
  62.     Gr.name = new char [Gr.n];
  63.     for (int i=0; i<Gr.n; i++)
  64.         inp >> Gr.name[i];
  65.     Gr.G = new int *[Gr.n];
  66.     Len = new int [Gr.n];
  67.     S = new int [Gr.n];
  68.     P = new int [Gr.n];
  69.    
  70.     for (int i=0; i<Gr.n; i++)
  71.     {
  72.         Gr.G[i] = new int [Gr.n];
  73.         for (int j=0; j<Gr.n; j++)
  74.             inp >> Gr.G[i][j];
  75.     }
  76.     inp.close();
  77. }
  78.  
  79. void input_B_E(int &a, int &b)
  80. {
  81.     a = b = 0;
  82.     cout<<endl<<"Cac dinh danh so tu 1 den "<<Gr.n<<".Hoac tu "<<Gr.name[0]<<" den "<<Gr.name[Gr.n-1]<<endl;
  83.     cout<<"Nhap dinh bat dau : ";
  84.     while (a<1 || a> Gr.n)
  85.     {
  86.         cin>>a;
  87.         if (a<1 || a> Gr.n)
  88.             cout<<"Khong hop le ! \nNhap lai dinh bat dau : ";
  89.     }
  90.    
  91.     cout<<"Nhap dinh ket thuc : ";
  92.     while (b<1 || b> Gr.n)
  93.     {
  94.         cin>>b;
  95.         if (b<1 || b> Gr.n)
  96.             cout<<"Khong hop le ! \nNhap lai dinh ket thuc : ";
  97.     }
  98.     a -- ;
  99.     b -- ;
  100. }
  101.  
  102. void output(int a, int b)
  103. {
  104.     cout<<endl<<"Do dai ngan nhat cua duong di tu "<<a+1<<"("<<Gr.name[a]<<")"
  105.         <<" den "<<b+1<<"("<<Gr.name[b]<<")"<<" la "<<Len[b]<<endl;
  106.     cout<<"Qua trinh duong di: ";
  107.     int i = b;
  108.     char *s, *temp;
  109.     s = new char [Gr.n*10];
  110.     temp = new char [10];
  111.     sprintf(s,"%d",i+1);
  112.     while (i != a)
  113.     {
  114.         sprintf(temp,"%s"," --> ");
  115.         strcpy(s,strcat(temp,s));
  116.         sprintf(temp,"%d",P[i] +1);
  117.         strcpy(s,strcat(temp,s));
  118.         i = P[i];
  119.     }  
  120.     cout<<s<<endl;
  121. }
  122.  
  123. //tong quang duong di cua moi dinh (thay the cho vo cung trong ma tran trong so)
  124. item tongthiethai()
  125. {
  126.     item sum = 0;
  127.     for (int i=0; i<Gr.n; i++)
  128.         for (int j=0; j<Gr.n; j++)
  129.             sum += Gr.G[i][j];
  130.     return sum;
  131. }
  132.  
  133. void Menu(int &select)
  134. {
  135.     cout<<"Tiep tuc (1/0) ?";
  136.     cin >> select;
  137. }
  138.  
  139. void Dijkstra(int a, int b)     // thuat toan Dijkstra
  140. {  
  141.     int max = tongthiethai();  
  142.     fill (Len,Len+Gr.n,max); //Gan duong di ban dau = vo cung
  143.     fill (P,P+Gr.n,a);
  144.     fill (S,S+Gr.n,0); //Danh sach dac biet
  145.     Len[a] = 0;     // khoi tao do dai tu a->a = 0
  146.     int i = a;
  147.    
  148.     //while S<>V
  149.     for (int k=0; k<Gr.n; k++)
  150.     {      
  151.         //tim do dai ngan nhat trong cac dinh      
  152.         for (i=0; i<Gr.n; i++) // tim v thuoc (V-S) va Len[v] < vo cung
  153.             if (!S[i] && Len[i] != max)
  154.                 break;
  155.         for (int j = i+1 ; j<Gr.n; j++) // tim dinh co Len min
  156.             if (!S[j] && Len[j] < Len[i])
  157.                 i = j;
  158.         S[i] = 1;   // dua dinh i vao S
  159.        
  160.         //--------Tinh do dai tu dinh dang xet toi cac dinh tiep
  161.         for (int j = 0; j<Gr.n; j++) //thay doi do dai neu co
  162.         {
  163.             if (!S[j] && Gr.G[i][j])
  164.                 if (Len[i] + Gr.G[i][j] < Len[j])
  165.                 {
  166.                     Len[j] = Len[i] + Gr.G[i][j];
  167.                     P[j] = i; //truy vet
  168.                 }
  169.         }              
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement