Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <string>
- #include <cstring>
- #include <algorithm>
- #include <fstream>
- #include <sstream>
- #include <queue>
- #include <stack>
- #define INP "input.INP"
- #define OUT "output.OUT"
- using namespace std;
- typedef int item;
- typedef struct GRAPH
- {
- char *name; // ten cac dinh
- item **G; // ma tran trong so
- int n; // so phan tu cua do thi
- } Graph;
- void input_file();// lay du lieu tu file
- void input_B_E(int &a, int &b); //nhap vao dinh dau va cuoi
- void output(int a, int b);//Xuat ket qua tu file ra
- void Dijkstra(int a, int b);//thuat toan Dijkstra
- void Menu(int &select);
- item tongthiethai(); //tong quang duong di cua moi dinh (thay the cho vo cung trong ma tran trong so)
- Graph Gr;
- int *Len;//Gia tri nho nhat tu a -> i. Len1 danh dau do dai.
- int *S;//Danh dau dinh thuoc danh sach dac biet
- int *P;//truy vet
- int main()
- {
- Graph Gr;
- input_file();
- int a, b, *P, i, select = 1;
- while (select)
- {
- cout<<endl<<"-----Thuat toan Dijkstra-----"<<endl;
- input_B_E(a, b);
- Dijkstra(a, b);
- output(a, b);
- Menu(select);
- if (!select) break;
- }
- return 0;
- }
- void input_file() // nhap ma tran ke tu file
- {
- ifstream inp(INP);
- if (inp == NULL)
- {
- cout<<"No found file input";
- return;
- }
- inp >> Gr.n ;
- Gr.name = new char [Gr.n];
- for (int i=0; i<Gr.n; i++)
- inp >> Gr.name[i];
- Gr.G = new int *[Gr.n];
- Len = new int [Gr.n];
- S = new int [Gr.n];
- P = new int [Gr.n];
- for (int i=0; i<Gr.n; i++)
- {
- Gr.G[i] = new int [Gr.n];
- for (int j=0; j<Gr.n; j++)
- inp >> Gr.G[i][j];
- }
- inp.close();
- }
- void input_B_E(int &a, int &b)
- {
- a = b = 0;
- cout<<endl<<"Cac dinh danh so tu 1 den "<<Gr.n<<".Hoac tu "<<Gr.name[0]<<" den "<<Gr.name[Gr.n-1]<<endl;
- cout<<"Nhap dinh bat dau : ";
- while (a<1 || a> Gr.n)
- {
- cin>>a;
- if (a<1 || a> Gr.n)
- cout<<"Khong hop le ! \nNhap lai dinh bat dau : ";
- }
- cout<<"Nhap dinh ket thuc : ";
- while (b<1 || b> Gr.n)
- {
- cin>>b;
- if (b<1 || b> Gr.n)
- cout<<"Khong hop le ! \nNhap lai dinh ket thuc : ";
- }
- a -- ;
- b -- ;
- }
- void output(int a, int b)
- {
- cout<<endl<<"Do dai ngan nhat cua duong di tu "<<a+1<<"("<<Gr.name[a]<<")"
- <<" den "<<b+1<<"("<<Gr.name[b]<<")"<<" la "<<Len[b]<<endl;
- cout<<"Qua trinh duong di: ";
- int i = b;
- char *s, *temp;
- s = new char [Gr.n*10];
- temp = new char [10];
- sprintf(s,"%d",i+1);
- while (i != a)
- {
- sprintf(temp,"%s"," --> ");
- strcpy(s,strcat(temp,s));
- sprintf(temp,"%d",P[i] +1);
- strcpy(s,strcat(temp,s));
- i = P[i];
- }
- cout<<s<<endl;
- }
- //tong quang duong di cua moi dinh (thay the cho vo cung trong ma tran trong so)
- item tongthiethai()
- {
- item sum = 0;
- for (int i=0; i<Gr.n; i++)
- for (int j=0; j<Gr.n; j++)
- sum += Gr.G[i][j];
- return sum;
- }
- void Menu(int &select)
- {
- cout<<"Tiep tuc (1/0) ?";
- cin >> select;
- }
- void Dijkstra(int a, int b) // thuat toan Dijkstra
- {
- int max = tongthiethai();
- fill (Len,Len+Gr.n,max); //Gan duong di ban dau = vo cung
- fill (P,P+Gr.n,a);
- fill (S,S+Gr.n,0); //Danh sach dac biet
- Len[a] = 0; // khoi tao do dai tu a->a = 0
- int i = a;
- //while S<>V
- for (int k=0; k<Gr.n; k++)
- {
- //tim do dai ngan nhat trong cac dinh
- for (i=0; i<Gr.n; i++) // tim v thuoc (V-S) va Len[v] < vo cung
- if (!S[i] && Len[i] != max)
- break;
- for (int j = i+1 ; j<Gr.n; j++) // tim dinh co Len min
- if (!S[j] && Len[j] < Len[i])
- i = j;
- S[i] = 1; // dua dinh i vao S
- //--------Tinh do dai tu dinh dang xet toi cac dinh tiep
- for (int j = 0; j<Gr.n; j++) //thay doi do dai neu co
- {
- if (!S[j] && Gr.G[i][j])
- if (Len[i] + Gr.G[i][j] < Len[j])
- {
- Len[j] = Len[i] + Gr.G[i][j];
- P[j] = i; //truy vet
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement