当前位置:网站首页>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;
}边栏推荐
- 怎么检查GBase 8c数据库中的锁信息?
- Minecraft 1.16.5 biochemical 8 module version 2.0 storybook + more guns
- [Wu Enda machine learning] week5 programming assignment EX4 - neural network learning
- 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
- 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
- GBase 8c数据库升级报错
- Publish your own toolkit notes using NPM
- Sword finger offer 30 Stack containing min function
- 2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
- 一位博士在华为的22年
猜你喜欢

The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup

Adapter-a technology of adaptive pre training continuous learning

PHP campus financial management system for computer graduation design

好用的 JS 脚本
![[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!](/img/aa/af98b588efd61d71b1b02609817c49.png)
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!

Multi function event recorder of the 5th National Games of the Blue Bridge Cup

Shell脚本更新存储过程到数据库

2022 edition illustrated network pdf

构建库函数的雏形——参照野火的手册

Using SA token to solve websocket handshake authentication
随机推荐
MySQL index
I like Takeshi Kitano's words very much: although it's hard, I will still choose that kind of hot life
Global and Chinese market of wheelchair climbing machines 2022-2028: Research Report on technology, participants, trends, market size and share
[solution] every time idea starts, it will build project
[depth first search notes] Abstract DFS
A basic lintcode MySQL database problem
Computer graduation design PHP college student human resources job recruitment network
Concept of storage engine
How to set an alias inside a bash shell script so that is it visible from the outside?
[coppeliasim] 6-DOF path planning
ftp上传文件时出现 550 Permission denied,不是用户权限问题
Sword finger offer 29 Print matrix clockwise
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
Global and Chinese markets for single beam side scan sonar 2022-2028: Research Report on technology, participants, trends, market size and share
Overview of spark RDD
SSM assembly
Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
sql表名作为参数传递
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
Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender