当前位置:网站首页>2020.02.11
2020.02.11
2022-07-06 02:24:00 【weisir1】
I learned it today djs Algorithm , Let's summarize .
This algorithm is to find the shortest path of a single source . Now I'll give you the template and ideas , Be clear at a glance .
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
const int N = 210;
const int INF = 0x3f3f3f3f; // infinity , Not adjacent is infinite
int G[N][N], dis[N]; // G Is the adjacency matrix ,dis Single source to i A weight
int p[N]; // Record the precursor
bool book[N]; // Determine whether it has been added to the set , The set represents the updated nodes
int n;
void dijkstra(int u, int n) // u It's the source , The shortest path from the source point to each vertex ,n It's a fixed number
{
for (int i = 1; i <= N; i++) // initialization
{
dis[i] = G[u][i];
book[i] = 0;
/*
if (dis[i] == INF) // If it is equal to INF, Express u Can't reach this vertex , The precursor is -1
p[i] = -1;
else
p[i] = u;
*/
}
dis[u] = 0; // u To u A weight of 0
book[u] = 1; // Express u Updated
for (int i = 1; i < n; i++) // Find the smallest and update dis
{
// printf("=============\n");
int min = INF, t = u;
for (int j = 1; j <= n; j++)
if (!book[j] && dis[j] < min) // If j Not updated and adjacent
{
min = dis[j];
t = j;
}
if (t == u) // Optimize
return;
book[t] = 1;
// printf("t:%d\n", t);
for (int j = 1; j <= n; j++)
{
if (!book[j] && dis[j] > dis[t] + G[t][j]) // If a vertex is at the same time and u and t Adjacency , Update dis[j] Value
{
// printf("dis[j]:%d\n", dis[j]);
dis[j] = dis[t] + G[t][j];
// p[j] = t; // change j The precursor of t
// printf("dis[j]:%d\n", dis[j]);
}
}
}
}
int main()
{
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
G[i][j] = INF;
cin >> n;
for (int i = 1; i <= n - 1; i++)
for (int j = i + 1; j <= n; j++)
cin >> G[i][j];
dijkstra(1, n);
cout << dis[3] << endl;
}边栏推荐
- [postgraduate entrance examination English] prepare for 2023, learn list5 words
- Know MySQL database
- Computer graduation design PHP college student human resources job recruitment network
- Gbase 8C database upgrade error
- Global and Chinese markets of general purpose centrifuges 2022-2028: Research Report on technology, participants, trends, market size and share
- Lecture 4 of Data Engineering Series: sample engineering of data centric AI
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- [community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
- How to check the lock information in gbase 8C database?
- 【机器人库】 awesome-robotics-libraries
猜你喜欢

Prepare for the autumn face-to-face test questions

SPI communication protocol

Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework

Using SA token to solve websocket handshake authentication

剑指 Offer 29. 顺时针打印矩阵

UE4 - how to make a simple TPS role (I) - create a basic role
![[robot library] awesome robots Libraries](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[robot library] awesome robots Libraries

【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
随机推荐
论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
Global and Chinese market of wheelchair climbing machines 2022-2028: Research Report on technology, participants, trends, market size and share
Sword finger offer 29 Print matrix clockwise
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
数据准备工作
Prepare for the autumn face-to-face test questions
ftp上传文件时出现 550 Permission denied,不是用户权限问题
Computer graduation design PHP part-time recruitment management system for College Students
Sword finger offer 30 Stack containing min function
Method of changing object properties
【clickhouse】ClickHouse Practice in EOI
Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
有沒有sqlcdc監控多張錶 再關聯後 sink到另外一張錶的案例啊?全部在 mysql中操作
高数_向量代数_单位向量_向量与坐标轴的夹角
Zero basic self-study STM32 wildfire review of GPIO use absolute address to operate GPIO
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
UE4 - how to make a simple TPS role (I) - create a basic role
【coppeliasim】6自由度路径规划
好用的 JS 脚本