当前位置:网站首页>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;
}
边栏推荐
- 在线怎么生成富文本
- Global and Chinese markets hitting traffic doors 2022-2028: Research Report on technology, participants, trends, market size and share
- Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
- LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
- 爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
- This time, thoroughly understand the deep copy
- 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
- PHP campus financial management system for computer graduation design
- sql表名作为参数传递
- [coppeliasim] 6-DOF path planning
猜你喜欢
vs code保存时 出现两次格式化
MySQL index
HttpRunnerManager安装(三)-Linux下配置myql数据库&初始化数据
Pangolin Library: subgraph
Jisuanke - t2063_ Missile interception
剑指 Offer 30. 包含min函数的栈
It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment
Social networking website for college students based on computer graduation design PHP
Know MySQL database
[depth first search notes] Abstract DFS
随机推荐
[depth first search notes] Abstract DFS
SPI communication protocol
SQL table name is passed as a parameter
Global and Chinese markets for single beam side scan sonar 2022-2028: Research Report on technology, participants, trends, market size and share
Jisuanke - t2063_ Missile interception
大厂镜像库
机器学习训练与参数优化的一般过程 (讨论)
SSM assembly
剑指 Offer 30. 包含min函数的栈
sql表名作为参数传递
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
Computer graduation design PHP part-time recruitment management system for College Students
Computer graduation design PHP campus restaurant online ordering system
How to generate rich text online
[solution] every time idea starts, it will build project
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
Template_ Quick sort_ Double pointer
MySQL (IV) - transactions
There are so many giants, why should we independently develop POS store cashier system?
I like Takeshi Kitano's words very much: although it's hard, I will still choose that kind of hot life