0% found this document useful (0 votes)
44 views5 pages

#Include Iostream

c++

Uploaded by

nguyenliem9514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

#Include Iostream

c++

Uploaded by

nguyenliem9514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <vector>

#include <unordered_map>

#include <set>

#include <limits>

using namespace std;

const int INF = numeric_limits<int>::max();

class CityMap

public:

unordered_map<int, vector<pair<int, int>>> roadMap;

void themDuong(int tu, int den, int khoangCach)

roadMap[tu].emplace_back(den, khoangCach);

roadMap[den].emplace_back(tu, khoangCach); // Ð?i v?i d? th? vô hu?ng

int dijkstra(int batDau, int ketThuc)

unordered_map<int, int> khoangCach;

set<pair<int, int>> dinh; // S? d?ng set d? t? d?ng s?p x?p theo kho?ng cách

for (const auto& entry : roadMap)

int dinhCuaDoThi = entry.first;

khoangCach[dinhCuaDoThi] = INF;

dinh.insert({INF, dinhCuaDoThi});

khoangCach[batDau] = 0;

dinh.erase({INF, batDau});

dinh.insert({0, batDau});

while (!dinh.empty())

{
int u = dinh.begin()->second;

dinh.erase(dinh.begin());

for (const auto& ke : roadMap[u])

int v = ke.first;

int trongSo = ke.second;

if (khoangCach[u] + trongSo < khoangCach[v]) {

dinh.erase({khoangCach[v], v});

khoangCach[v] = khoangCach[u] + trongSo;

dinh.insert({khoangCach[v], v});

return khoangCach[ketThuc];

vector<int> timDuongNganNhat(int batDau, int ketThuc)

unordered_map<int, int> khoangCach;

unordered_map<int, int> truoc;

set<pair<int, int>> dinh;

for (const auto& entry : roadMap)

int dinhCuaDoThi = entry.first;

khoangCach[dinhCuaDoThi] = INF;

truoc[dinhCuaDoThi] = -1;

dinh.insert({INF, dinhCuaDoThi});

khoangCach[batDau] = 0;

dinh.erase({INF, batDau});

dinh.insert({0, batDau});

while (!dinh.empty())
{

int u = dinh.begin()->second;

dinh.erase(dinh.begin());

for (const auto& ke : roadMap[u])

int v = ke.first;

int trongSo = ke.second;

if (khoangCach[u] + trongSo < khoangCach[v])

dinh.erase({khoangCach[v], v});

khoangCach[v] = khoangCach[u] + trongSo;

truoc[v] = u;

dinh.insert({khoangCach[v], v});

// Ki?m tra xem có du?ng di t? batDau d?n ketThuc không

if (khoangCach[ketThuc] == INF)

cout << "Không có du?ng di t? " << batDau << " d?n " << ketThuc << endl;

return {};

// T?o du?ng di ng?n nh?t t? batDau d?n ketThuc

vector<int> duongDi;

int hienTai = ketThuc;

while (hienTai != -1)

duongDi.insert(duongDi.begin(), hienTai);

hienTai = truoc[hienTai];

return duongDi;
}

};

int main()

CityMap thanhPho;

// Thêm các con du?ng vào b?n d?

thanhPho.themDuong(1, 2, 5);

thanhPho.themDuong(1, 3, 3);

thanhPho.themDuong(2, 3, 2);

thanhPho.themDuong(2, 4, 8);

thanhPho.themDuong(3, 4, 1);

// Thêm ngã 3 và ngã 4 vào b?n d?

thanhPho.themDuong(5, 3, 4);

thanhPho.themDuong(6, 4, 5);

// Hi?n th? b?n d? và các l?a ch?n

cout << "B?n d?:" << endl;

cout << "1---2" << endl;

cout << "| |" << endl;

cout << "3---4" << endl;

cout << " \\ /" << endl;

cout << " 5" << endl;

cout << " \\" << endl;

cout << " 6" << endl;

// Ngu?i dùng ch?n v? trí hi?n t?i, di?m dón khách, và di?m dích

int viTriHienTai, viTriDonKhach, viTriDich;

cout << "Nh?p v? trí hi?n t?i (1-6): ";

cin >> viTriHienTai;

cout << "Nh?p v? trí dón khách (1-6): ";

cin >> viTriDonKhach;

cout << "Nh?p v? trí dích (1-6): ";


cin >> viTriDich;

// Tìm du?ng di t? v? trí hi?n t?i d?n di?m dón khách

vector<int> duongDiDenDonKhach = thanhPho.timDuongNganNhat(viTriHienTai, viTriDonKhach);

// Tìm du?ng di t? di?m dón khách d?n di?m dích

vector<int> duongDiDenDich = thanhPho.timDuongNganNhat(viTriDonKhach, viTriDich);

// In du?ng di t? v? trí hi?n t?i d?n di?m dón khách

cout << "Ðu?ng di ng?n nh?t t? v? trí hi?n t?i d?n di?m dón khách: ";

for (int dinh : duongDiDenDonKhach)

cout << dinh << " ";

cout << endl;

// In du?ng di t? di?m dón khách d?n di?m dích

cout << "Ðu?ng di ng?n nh?t t? di?m dón khách d?n di?m dích: ";

for (int dinh : duongDiDenDich)

cout << dinh << " ";

cout << endl;

return 0;

You might also like