Advertisement
nguyenvanquan7826

Floyd

Oct 12th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 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 Floyd (int a, int b);//thuat toan Floyd
  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 **P;//truy vet
  33. int **A;
  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 Floyd-----"<<endl;
  43.         input_B_E(a, b);
  44.         Floyd(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.     P = new int *[Gr.n];
  68.     A = new int *[Gr.n];
  69.     for (int i=0; i<Gr.n; i++)
  70.     {
  71.         P[i] = new int [Gr.n];
  72.         A[i] = new int [Gr.n];
  73.         Gr.G[i] = new int [Gr.n];
  74.         for (int j=0; j<Gr.n; j++)
  75.             inp >> Gr.G[i][j];
  76.     }
  77.        
  78.     inp.close();
  79. }
  80.  
  81. void input_B_E(int &a, int &b)
  82. {
  83.     a = b = 0;
  84.     cout<<endl<<"Cac dinh danh so tu 1 den "<<Gr.n<<".Hoac tu "<<Gr.name[0]<<" den "<<Gr.name[Gr.n-1]<<endl;
  85.     cout<<"Nhap dinh bat dau : ";
  86.     while (a<1 || a> Gr.n)
  87.     {
  88.         cin>>a;
  89.         if (a<1 || a> Gr.n)
  90.             cout<<"Khong hop le ! \nNhap lai dinh bat dau : ";
  91.     }
  92.    
  93.     cout<<"Nhap dinh ket thuc : ";
  94.     while (b<1 || b> Gr.n)
  95.     {
  96.         cin>>b;
  97.         if (b<1 || b> Gr.n)
  98.             cout<<"Khong hop le ! \nNhap lai dinh ket thuc : ";
  99.     }
  100.     a -- ;
  101.     b -- ;
  102. }
  103.  
  104. void output(int a, int b)
  105. {
  106.     cout<<endl<<"Do dai ngan nhat cua duong di tu "<<a+1<<"("<<Gr.name[a]<<")"<<" den "<<b+1<<"("<<Gr.name[b]<<")"<<" la "<<A[a][b]<<endl;
  107.     cout<<"Qua trinh duong di: ";
  108.    
  109.     //truy vet
  110.     char *s, *temp;
  111.     s = new char [Gr.n*10];
  112.     temp = new char [10];
  113.     stack <item> S1;
  114.     stack <item> S2;
  115.     S1.push(a); //danh sach nap cac dinh vao
  116.     S1.push(b); //danh sach xuat cac dinh ra
  117.     int dich, tg;
  118.     while (!S1.empty())
  119.     {
  120.         dich = S1.top(); //dich = phan tu dau tien
  121.         S1.pop();       // dua phan tu do ra
  122.         S2.push(dich); //cho vao danh sach xuat
  123.         if (!S1.empty()) //trong khi S1 ko rong thi tiep tuc tim cac dinh
  124.         {
  125.             tg = S1.top();
  126.             while (P[tg][dich] != -1) //tim cac dinh di tu tg den dich
  127.             {
  128.                 S1.push(P[tg][dich]);
  129.                 tg = S1.top();
  130.             }
  131.         }
  132.     }
  133.    
  134.     sprintf(s,"%d",S2.top()+1);
  135.     S2.pop();
  136.    
  137.     while (!S2.empty())
  138.     {
  139.         sprintf(temp,"%s%d"," --> ",S2.top()+1);
  140.         strcat(s,temp);
  141.         S2.pop();
  142.     }
  143.     cout<<s<<endl;
  144. }
  145.  
  146. //tong quang duong di cua moi dinh (thay the cho vo cung trong ma tran trong so)
  147. item tongthiethai()
  148. {
  149.     item sum = 0;
  150.     for (int i=0; i<Gr.n; i++)
  151.         for (int j=0; j<Gr.n; j++)
  152.             sum += Gr.G[i][j];
  153.     return sum;
  154. }
  155.  
  156. void Menu(int &select)
  157. {
  158.     cout<<"Tiep tuc (1/0) ?";
  159.     cin >> select;
  160. }
  161.  
  162. void Floyd (int a, int b)
  163. {
  164.     int max = tongthiethai();
  165.     for (int i=0; i<Gr.n; i++)
  166.         for (int j=0; j<Gr.n; j++)
  167.         {
  168.             if (Gr.G[i][j])
  169.                 A[i][j] = Gr.G[i][j];
  170.             else A[i][j] = max;
  171.             P[i][j] = -1;
  172.         }
  173.    
  174.     for (int k=0; k<Gr.n; k++)  // lap n lan
  175.     {  
  176.         for (int i=0; i<Gr.n; i++)  // thay doi do dai duong di cua cac dinh
  177.             for (int j=0; j<Gr.n; j++)
  178.                 if (A[i][j] > A[i][k] + A[k][j])
  179.                 {
  180.                     A[i][j] = A[i][k] + A[k][j];
  181.                     P[i][j] = k ;
  182.                 }  
  183.     }  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement