0% found this document useful (0 votes)
8 views

D1

The document contains code for implementing Dijkstra's algorithm to find the shortest path between nodes in a graph. It defines structures to represent a graph with nodes and edges, reads in a graph from a file, runs Dijkstra's algorithm to calculate the shortest distances and predecessors for each node, and outputs the shortest path between the start and end nodes. Key steps include initializing data structures, running Dijkstra's algorithm in a loop to relax edges and update distances/predecessors, and tracing the predecessors to output the shortest path.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

D1

The document contains code for implementing Dijkstra's algorithm to find the shortest path between nodes in a graph. It defines structures to represent a graph with nodes and edges, reads in a graph from a file, runs Dijkstra's algorithm to calculate the shortest distances and predecessors for each node, and outputs the shortest path between the start and end nodes. Key steps include initializing data structures, running Dijkstra's algorithm in a loop to relax edges and update distances/predecessors, and tracing the predecessors to output the shortest path.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

D1;

#include <iostream>
#include <string>
using namespace std;
struct khach
{
int ma;
string ten;
string sdt;
float tong;
};
typedef struct khach k;
void nhap(khach *&x , int n)
{
for(int i = 0 ;i < n;i++)
{
cout<<"\nnhap ma : ";cin>>x[i].ma;cin.ignore();
cout<<"nhap ten: ";getline(cin,x[i].ten);
cout<<"nhap sdt: ";getline(cin,x[i].sdt);
cout<<"nhap tong: ";cin>>x[i].tong;cin.ignore();
}
}
void xuat(khach *x , int n)
{
for(int i = 0 ; i < n;i++)
{
cout<<" - ma : "<<x[i].ma
<<" \t- ten : "<<x[i].ten
<<" \t- sdt "<<x[i].sdt
<<"\t- tong:"<<x[i].tong
<<endl;
}
}
void inserttion( khach *&x,int n)
{
khach k;
int pos;
for(int i= 0 ; i<n ; i++){
k = x[i];
pos = i-1;
while(pos >= 0 && x[pos].tong > k.tong){
x[pos+1]=x[pos];
pos--;
}
k = x[pos+1];
}
}

void BubbleSort( khach *&x, int n)


{

int i , j;
for (i=0;i<n-1;i++)
for (j=n-1;j>i; j--)
if (x[j].tong< x[j-1].tong)
swap(x[j],x[j-1]);
}
void selectionsort ( khach *&x, int n)
{
int i, j,min ;
for (i=0;i<n-1;i++)
{
min=i;
for (j=i+1;j<n;j++)
if (x[min].tong>x[j].tong)
min = j;
if (min!=i)
swap(x[min].tong, x[i].tong);
}
}
void quicksort( khach *&x, int n)
{

int i , j;
for (i=0;i<n-1;i++)
for (j=n-1;j>i; j--)
if (x[j].tong< x[j-1].tong)
swap(x[j].tong,x[j-1].tong);
}
int main()
{
int n;
cout<<"nhap so khach hang la : ";cin>>n;
khach *k= new khach[n];
nhap(k , n);
printf("\nham vua nhap la:\n");
xuat(k , n);
cout<<"\nsau khi sap xep theo kieu bubble : ";
BubbleSort(k , n);
cout<<"\n";
xuat(k , n);
cout<<"\nsau khi sap xep theo kieu selection : ";
selectionsort(k , n);
cout<<"\n";
xuat(k , n);
cout<<"\nsau khi sap xep theo kieu quick : ";
quicksort(k , n);
cout<<"\n";
xuat(k , n);
cout<<"\nSau khi sap xep theo kieu insert: ";
inserttion(k,n);
cout<<"\n";
xuat(k,n);
}
D2;
#include<iostream>
#include<string>

using namespace std;

struct HangHoa{
int maH;
string tenH;
int ngay, thang, nam;
int gia;
};
typedef HangHoa HH;
HH Nhap(){
HangHoa hanghoa;
cout<<"-Ma hang:";
cin>>hanghoa.maH;
cout<<"-Ten hang:";fflush(stdin);
getline(cin,hanghoa.tenH);
cout<<"-Ngay:";cin>>hanghoa.ngay;
cout<<"-Thang:";cin>>hanghoa.thang;
cout<<"-Nam:";cin>>hanghoa.nam;
cout<<"-Gia :"; cin>>hanghoa.gia;

return hanghoa;
}

struct Node{
HangHoa hanghoa;
struct Node *left;
struct Node *right;
};

typedef struct Node *BinaryTree;


typedef struct Node *NodePtr;

void PrintNode(NodePtr p){


cout<<"Ma hang:"<<p->hanghoa.maH<<" | "
<<"Ten hang:"<<p->hanghoa.tenH<<" | "
<<"Ngay xuat:"<<p->hanghoa.ngay<<"/"<<p-
>hanghoa.thang<<"/"<<p->hanghoa.nam<<" |"
<<"Gia:"<<p->hanghoa.gia<<endl;
}

void initialize(BinaryTree &T){


T=NULL;
}

NodePtr createNode(HangHoa hanghoa){


NodePtr newNode = new Node;
newNode->left=NULL;
newNode->right=NULL;
newNode->hanghoa=hanghoa;
return newNode;
}

void insertToTree(BinaryTree &T, HangHoa hanghoa){


NodePtr q;
if(T==NULL){
q=createNode(hanghoa);
T=q;
}
else if(hanghoa.maH < T->hanghoa.maH)
insertToTree(T->left, hanghoa);
else if(hanghoa.maH > T->hanghoa.maH)
insertToTree(T->right, hanghoa);

}
NodePtr search(int maH, BinaryTree T){
NodePtr p;
p=T;
if(p!=NULL){
if(maH < p->hanghoa.maH)
return search(maH,p->left);
else if(maH > p->hanghoa.maH)
return search(maH, p->right);
else return p;
}
else return NULL;
}

void inOrder(BinaryTree T){


if(T!=NULL){
inOrder(T->left);
PrintNode(T);
inOrder(T->right);
}
}

int main(){
int n;
cout<<"Nhap so luong hang hoa:";
cin>>n;
BinaryTree T;
initialize(T);
for(int i=0;i<n;i++){
cout<<"Nhap thong tin hang hoa thu "<<i+1<<":"<<endl;
HangHoa hanghoa = Nhap();
insertToTree(T,hanghoa);
}

cout<<"Hien thi thong tin hang hoa:"<<endl;


inOrder(T);

int maH;
cout<<"Nhap ma hang can tim kiem:";
cin>>maH;

NodePtr found=search(maH,T);
if(found==NULL)
cout<<"Khong tim thay hang hoa co ma "<<maH<<endl;
else{
cout<<"Tim thay hang hoa co ma "<<maH<<endl;
PrintNode(found);
}
}
D3;
#include<iostream>

using namespace std;

#define MAX 50

#define TRUE 1
#define FALSE 0

#define VOCUNG 10000000

int n; //s? d?nh c?a d? th?

int s; //d?nh d?u


int t; //d?nh cu?i

int truoc[MAX]; //m?ng dánh d?u du?ng di


int d[MAX]; //m?ng dánh d?u kho?ng cách
int Matrix[MAX][MAX]; //ma tr?n tr?ng s?
int T[MAX]; //m?ng dánh d?u d?nh dã du?c gán nhãn

void Nhap()
{
freopen("M.TXT","r", stdin);
cin>>n;
cout<<"So dinh : "<< n<<endl;
cin>>s>>t; //nh?p d?nh d?u và d?nh cu?i c?a d? th?
//nh?p ma tr?n c?a d? th?
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin>>Matrix[i][j];
if (Matrix[i][j] == 0) Matrix[i][j] = VOCUNG;
}
}
}

void HienThi()
{
cout<<"Duong di ngan nhat tu "<<(char)(s+'A'-1)<<" den "<<(char)(t +
'A' -1)<< " la"<<endl;
cout<<(char)(t + 'A' - 1)<<"<="; //in d?nh cu?i du?i d?ng char
int i = truoc[t];
while (i != s)
{
cout<<(char)(i +'A' -1)<<"<="; //in ra k?t qu? du?i d?ng char
i = truoc[i];
}
cout<<(char)(s+'A' -1); //in d?nh d?u du?i d?ng char
cout<<endl<<"Do dai duong di la : "<< d[t];
}

void Dijkstra()
{
int u, minp;
//kh?i t?o nhãn t?m th?i cho các d?nh
for (int v = 1; v <= n; v++)
{
d[v] = Matrix[s][v];
truoc[v] = s;
T[v] = FALSE;
}
truoc[s] = 0;
d[s] = 0;
T[s] = TRUE;

//bu?c l?p
while (!T[t])
{
minp = VOCUNG;
//tìm d?nh u sao cho d[u] là nh? nh?t
for (int v = 1; v <= n; v++)
{
if ((!T[v]) && (minp > d[v]))
{
u = v;
minp = d[v];
}
}
T[u] = TRUE; // u la dinh co nhan tam thoi nho nhat

//gán nhãn l?i cho các d?nh.


for (int v = 1; v <= n; v++)
{
if ((!T[v]) && (d[u] + Matrix[u][v] < d[v]))
{
d[v] = d[u] + Matrix[u][v];
truoc[v] = u;
}
}
}
}
int main()
{
Nhap();
Dijkstra();
HienThi();
}

You might also like