Share c++ rat source code

crazyduck479

New member
#C ++ #Rat #Source#Programming #algorithms

## mã nguồn chuột C ++

Thuật toán chuột là một thuật toán đệ quy tìm thấy đường dẫn ngắn nhất giữa hai đỉnh trong biểu đồ acyclic có hướng (DAG).Thuật toán hoạt động bằng cách lặp theo các cạnh của biểu đồ, bắt đầu từ đỉnh nguồn và di chuyển về phía đỉnh đích.Ở mỗi bước, thuật toán chọn cạnh dẫn đến đỉnh với khoảng cách nhỏ nhất từ đỉnh nguồn.Thuật toán chấm dứt khi nó đến đỉnh đích.

Mã nguồn C ++ cho thuật toán chuột được hiển thị bên dưới.

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// Một cấu trúc để đại diện cho một đỉnh trong một DAG.
struct vertex {
ID int;
Vector <Int> cạnh;
};

// một hàm để tìm đường dẫn ngắn nhất giữa hai đỉnh trong DAG.
int findShortestPath (đỉnh* đỉnh, int nguồn, điểm đến int) {
// Tạo một vectơ để lưu trữ khoảng cách từ đỉnh nguồn cho các đỉnh khác.
Vector <Int> khoảng cách (đỉnh-> kích thước (), int_max);

// Đặt khoảng cách từ đỉnh nguồn thành chính nó thành 0.
khoảng cách [nguồn] = 0;

// Tạo một hàng đợi để lưu trữ các đỉnh đang được xử lý.
Hàng đợi <Int> hàng đợi;

// Thêm đỉnh nguồn vào hàng đợi.
hàng đợi.push (nguồn);

// Trong khi hàng đợi không trống, tiếp tục xử lý các đỉnh.
while (! hàng đợi.empty ()) {
// Nhận đỉnh tiếp theo từ hàng đợi.
int currentvertex = hàng đợi.front ();
hàng đợi.pop ();

// Đối với mỗi cạnh của đỉnh hiện tại, hãy cập nhật khoảng cách từ đỉnh nguồn đến đỉnh đích.
for (int i = 0; i <đỉnh [currentvertex] .edges.size (); i ++) {
int nextvertex = đỉnh [currentvertex] .edges ;
if (khoảng cách [nextvertex]> khoảng cách [currentvertex] + 1) {
khoảng cách [nextvertex] = khoảng cách [currentvertex] + 1;
hàng đợi.push (nextvertex);
}
}
}

// Trả lại khoảng cách từ đỉnh nguồn cho đỉnh đích.
Trả lại khoảng cách [đích];
}

int main () {
// Tạo một vectơ của các đỉnh.
vector <ertex> đỉnh = {
{0, {1, 2}},
{1, {0, 3}},
{2, {0, 3}},
{3, {1, 2}}
};

// Tìm đường dẫn ngắn nhất giữa các đỉnh 0 và 3.
int shortestpath = findShortestPath (đỉnh, 0, 3);

// In đường dẫn ngắn nhất.
cout << "Đường dẫn ngắn nhất từ đỉnh 0 đến đỉnh 3 là" << shortestpath << endl;

trả lại 0;
}
`` `

## hashtags

* #C ++
* #Con chuột
* #mã nguồn
* #Programming
* #algorithms
=======================================
#C++ #Rat #Source Code #Programming #algorithms

## C++ Rat Source Code

The Rat algorithm is a recursive algorithm that finds the shortest path between two vertices in a directed acyclic graph (DAG). The algorithm works by iteratively following the edges of the graph, starting from the source vertex and moving towards the destination vertex. At each step, the algorithm chooses the edge that leads to the vertex with the smallest distance from the source vertex. The algorithm terminates when it reaches the destination vertex.

The C++ source code for the Rat algorithm is shown below.

```c++
#include <iostream>
#include <vector>

using namespace std;

// A struct to represent a vertex in a DAG.
struct Vertex {
int id;
vector<int> edges;
};

// A function to find the shortest path between two vertices in a DAG.
int findShortestPath(Vertex* vertices, int source, int destination) {
// Create a vector to store the distances from the source vertex to each other vertex.
vector<int> distances(vertices->size(), INT_MAX);

// Set the distance from the source vertex to itself to 0.
distances[source] = 0;

// Create a queue to store the vertices that are being processed.
queue<int> queue;

// Add the source vertex to the queue.
queue.push(source);

// While the queue is not empty, continue processing vertices.
while (!queue.empty()) {
// Get the next vertex from the queue.
int currentVertex = queue.front();
queue.pop();

// For each edge of the current vertex, update the distance from the source vertex to the destination vertex.
for (int i = 0; i < vertices[currentVertex].edges.size(); i++) {
int nextVertex = vertices[currentVertex].edges;
if (distances[nextVertex] > distances[currentVertex] + 1) {
distances[nextVertex] = distances[currentVertex] + 1;
queue.push(nextVertex);
}
}
}

// Return the distance from the source vertex to the destination vertex.
return distances[destination];
}

int main() {
// Create a vector of vertices.
vector<Vertex> vertices = {
{0, {1, 2}},
{1, {0, 3}},
{2, {0, 3}},
{3, {1, 2}}
};

// Find the shortest path between vertices 0 and 3.
int shortestPath = findShortestPath(vertices, 0, 3);

// Print the shortest path.
cout << "The shortest path from vertex 0 to vertex 3 is " << shortestPath << endl;

return 0;
}
```

## Hashtags

* #C++
* #Rat
* #Source Code
* #Programming
* #algorithms
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top