当前位置:网站首页>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;
}边栏推荐
- [robot library] awesome robots Libraries
- Black high-end responsive website dream weaving template (adaptive mobile terminal)
- 机器学习训练与参数优化的一般过程 (讨论)
- 【无标题】数据库中一条查询SQL执行的过程
- PAT甲级 1033 To Fill or Not to Fill
- Executing two identical SQL statements in the same sqlsession will result in different total numbers
- VIM usage guide
- Formatting occurs twice when vs code is saved
- Virtual machine network, networking settings, interconnection with host computer, network configuration
- 【clickhouse】ClickHouse Practice in EOI
猜你喜欢

How to improve the level of pinduoduo store? Dianyingtong came to tell you

Know MySQL database

Online reservation system of sports venues based on PHP

Adapter-a technology of adaptive pre training continuous 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

Building the prototype of library functions -- refer to the manual of wildfire

A basic lintcode MySQL database problem
![Grabbing and sorting out external articles -- status bar [4]](/img/1e/2d44f36339ac796618cd571aca5556.png)
Grabbing and sorting out external articles -- status bar [4]

【clickhouse】ClickHouse Practice in EOI

Spark accumulator
随机推荐
SPI communication protocol
2022 China eye Expo, Shandong vision prevention and control exhibition, myopia, China myopia correction Exhibition
[coppeliasim] 6-DOF path planning
Shell脚本更新存储过程到数据库
Jisuanke - t2063_ Missile interception
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
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
Initial understanding of pointer variables
我把驱动换成了5.1.35,但是还是一样的错误,我现在是能连成功,但是我每做一次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
【机器人库】 awesome-robotics-libraries
sql表名作为参数传递
PHP campus financial management system for computer graduation design
Structural theme model (I) STM package workflow
PAT甲级 1033 To Fill or Not to Fill
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
This time, thoroughly understand the deep copy
Template_ Quick sort_ Double pointer
好用的 JS 脚本