当前位置:网站首页>Search and graph theory: Dijkstra finding the shortest path i-dijkstra (shortest path)
Search and graph theory: Dijkstra finding the shortest path i-dijkstra (shortest path)
2022-06-11 16:13:00 【Fight! Sao Nian!】
subject :AcWing 849. Dijkstra Find the shortest path I
Given a n A little bit m Directed graph of strip edge , There may be double edges and self rings in the graph , All edge weights are positive .
Please find out 1 It's on the n The shortest distance of point No , If you can't get from 1 Go to No n Number point , The output −1.
Input format
The first line contains integers n and m.
Next m Each row contains three integers x,y,z, Indicates that there is a from point x point-to-point y The directed side of , Side length is z.
Output format
Output an integer , Express 1 It's on the n The shortest distance of point No .
If the path doesn't exist , The output −1.
Data range
1≤n≤500,
1≤m≤105,
The side length involved in the figure does not exceed 10000.
sample input :
3 3
1 2 2
2 3 1
1 3 4
sample output :
3
Simple dijastra algorithm :
Conduct n Secondary cycle ,
1. First step : Find an undetermined point , And closest to the starting point ( greedy ) Prove slightly
2. The second step : Update all remaining points to the current point , Because many points can reach the starting point through this point .
3. The third step : Mark this point as a definite point
#include <iostream>
#include <cstring>
using namespace std;
const int N = 510;
int g[N][N]; // Adjacency matrix
int dist[N]; // Shortest path
bool st[N]; // Judge whether the point is the shortest
int n,m;
int dijkstra()
{
memset(dist,0x3f,sizeof(dist));
dist[1]=0; // The distance of the first point is 0
for(int i=0;i<n-1;i++)
{
int t=-1;
for(int j=1;j<=n;j++) // Find an undetermined point , And closest to the starting point ( greedy )
if(!st[j]&&(t==-1||dist[t]>dist[j]))
t=j;
for(int j=1;j<=n;j++) // Update the distance between all points and the current point
dist[j]=min(dist[j],dist[t]+g[t][j]);
st[t]=true; // Set the current point as the determined point
}
if(dist[n]==0x3f3f3f3f)return -1;
return dist[n];
}
int main()
{
cin>>n>>m;
memset(g,0x3f,sizeof(g)); // Initialization distance
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
g[a][b]=min(g[a][b],c); // Ensure that the adjacency matrix is the shortest distance
}
int k=dijkstra();
cout<<k<<endl;
return 0;
}
边栏推荐
- Ai4db: AI slow SQL root cause analysis
- 干掉 Swagger UI,这款神器更好用、更高效!
- Application of AI in index recommendation
- Discussion on opengauss parallel decoding
- Transfer learning
- laravel 8 通过 任务调度 实现 数据库备份
- Open the door of the hybrid cloud market, Lenovo xcloud's way to break the situation
- 大龄码农从北京到荷兰的躺平生活
- Dapr mind map
- 完整的测试流程【杭州多测师】【杭州多测师_王sir】
猜你喜欢

1267_FreeRTOS启动第一个任务接口prvPortStartFirstTask实现分析

完整的测试流程【杭州多测师】【杭州多测师_王sir】

laravel 监听模式

Aaai2022 latest "time series data processing" report, 127 pages of PPT describing time series data processing and medical application progress

DHCP protocol instantiation analysis

Streaking? Baa!

无心剑英汉双语诗001. 《春游》

Project workspace creation steps - Zezhong ar automated test tool
![[Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster](/img/bc/694f8baec114cc3f6e35c4c76c29f0.png)
[Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster

Nat Commun|语言模型可以学习复杂的分子分布
随机推荐
Memory optimization table mot management
类的 prototype 属性和__proto__属性,类原型链有两条继承路线
From repeatedly rejected manuscripts to post-90s assistant professor, Wang Hao of Rutgers University: curiosity drives me to constantly explore
TC8:UDP_MessageFormat_01-02
GO語言-值類型和引用類型
[Yugong series] June 2022 Net architecture class 076- execution principle of distributed middleware schedulemaster
Aaai2022 latest "time series data processing" report, 127 pages of PPT describing time series data processing and medical application progress
What happened to the frequent disconnection of the computer at home
Selenium-- display waiting (medium) -- detailed explanation
Nat Commun|语言模型可以学习复杂的分子分布
DHCP协议实例化分析
Time processing logic for the last 7 days, the last 10 days, and the last 90 days
PyQt5 使QPlainTextEdit控件支持行号显示
Nat Commun|語言模型可以學習複雜的分子分布
laravel 8 使用passport 进行Auth验证及颁发token
Understand the dense support functions / stored procedures of opengauss
Using cloud DB to build apps quick start - quick games
Learn automatic testing of postman interface from 0 to 1
Connect to the database using GSQL
Classmate, have you heard of mot?