Day Con Tang Dai Nhat: Tam Tat Ca To Hop Con
Day Con Tang Dai Nhat: Tam Tat Ca To Hop Con
Day Con Tang Dai Nhat: Tam Tat Ca To Hop Con
#include <iostream>
#include <vector>
using namespace std;
Enqueue
#include <iostream>
#include <queue>
#include <stack>
int main() {
std::queue<int> myQueue;
for (int i = 1; i <= 5; i++) {
myQueue.push(i);
std::cout << "Enqueued: " << i << std::endl;
}
while (!myQueue.empty()) {
int frontElement = myQueue.front();
std::cout << "Dequeued: " << frontElement << std::endl;
myQueue.pop();
}
std::cout << std::endl;
std::stack<int> myStack;
for (int i = 1; i <= 5; i++) {
myStack.push(i);
std::cout << "Pushed: " << i << std::endl;
}
while (!myStack.empty()) {
int topElement = myStack.top();
std::cout << "Popped: " << topElement << std::endl;
myStack.pop();
}
return 0;
}
Dijiktra
#include <iostream>
#include <vector>
#include <climits>
return dinhNhoNhat;
}
graph[dinhNguon].khoangCach = 0;
int main() {
int soDinh, soCanh;
cout << "Nhap so dinh va so canh cua do thi: ";
cin >> soDinh >> soCanh;
vector<Dinh> graph(soDinh);
cout << "Nhap cac canh cua do thi (dinh nguon, dinh dich, trong so):\n";
for (int i = 0; i < soCanh; i++) {
int dinhNguon, dinhDich, trongSo;
cin >> dinhNguon >> dinhDich >> trongSo;
graph[dinhNguon].ke.push_back({dinhDich, trongSo});
}
dijkstra(graph, dinhNguon);
cout << "Khoang cach ngan nhat tu " << dinhNguon << " den " << dinhDich << " la:
" << graph[dinhDich].khoangCach << endl;
cout << "Duong di ngan nhat tu " << dinhNguon << " den " << dinhDich << " la: ";
inDuongDi(graph, dinhNguon, dinhDich);
return 0;
}
So xuat hien 2 lan
#include <iostream>
#include <vector>
#include <unordered_map>
#include <climits>
if (viTriCuoi.find(prefixSums[i]) != viTriCuoi.end()) {
soNhoNhat = min(soNhoNhat, mang[viTriCuoi[prefixSums[i]]]);
}
viTriCuoi[prefixSums[i]] = i - 1;
}
return soNhoNhat;
}
int main() {
int n;
cout << "Nhap so luong phan tu cua mang: ";
cin >> n;
vector<int> mang(n);
cout << "Nhap cac phan tu cua mang:\n";
for (int i = 0; i < n; ++i) {
cin >> mang[i];
}
if (ketQua != INT_MAX) {
cout << "So nho nhat xuat hien it nhat hai lan la: " << ketQua << endl;
} else {
cout << "Khong co so nao xuat hien it nhat hai lan trong mang." << endl;
}
return 0;
}