当前位置:网站首页>Dijkstra find the shortest path
Dijkstra find the shortest path
2022-07-26 01:23:00 【zjsru_ Beginner】
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≤10^5,
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 :
3Description by topic , The total is n A little bit ,m side ,n,m The data range of is greater than 100 times , Belong to A dense picture , And all edge weights in the graph are positive , So we can use it simple Dijkstra Algorithm To find the shortest path problem
Dense graphs use Adjacency matrix for storage And every point is initially infinite
Use an array to store 1 The shortest path to this location
Use one bool An array of types , To determine whether the location has been determined as the shortest path
Update the path from each point to the adjacent point in turn every time , And take the minimum
If a double edge occurs , Keep his shortest side
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=510;
int g[N][N];// Adjacency matrix
int dis[N];// Store the shortest path
bool st[N];// Judge whether it has been judged
int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for (int i = 0; i < n - 1; i ++ )
{
int t = -1;
for (int j = 1; j <= n; j ++ )
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
for (int j = 1; j <= n; j ++ )
dist[j] = min(dist[j], dist[t] + g[t][j]);
st[t] = true;
}
if (dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}
int main()
{
scanf("%d%d", &n, &m);
memset(g, 0x3f, sizeof g);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a][b] = min(g[a][b], c);
}
printf("%d\n", dijkstra());
return 0;
}
GYX
边栏推荐
- MDK compilation process and arm compilation tool chain
- [software development specification II] prohibited item development specification
- Iftnews | suppose this is what the metauniverse looks like 20 years later
- Matlab bitwise and or not
- Tutorial on principles and applications of database system (054) -- MySQL query (16): usage of date time function
- 如何获取广告服务流量变现数据,助力广告效果分析?
- Nodejs builds cloud native microservice applications based on dapr, a quick start guide from 0 to 1
- 数据库系统原理与应用教程(053)—— MySQL 查询(十五):字符型函数的用法
- 谷歌浏览器调试工具使用基础版(一)
- Android SQLite first groups and then sorts left concatenated queries
猜你喜欢
随机推荐
Inverse matrix block matrix
Sqli-labs Less7
[software development specification III] [software version naming Specification]
《nlp入门+实战:第三章:梯度下降和反向传播 》
[Go]三、最简单的RestFul API服务器
手游多开用模拟器多开游戏如何切换IP搬砖
【数据挖掘】生成模型和判别模型的区别及优缺点
换ip软件的用途很广及原理 动态IP更换的四种方法来保护网络隐私
【ICKIM 2022】第四届知识与信息管理国际会议
Iftnews | suppose this is what the metauniverse looks like 20 years later
[data mining] differences, advantages and disadvantages between generative model and discriminant model
Failed to load DLL
Advanced C language (I) dynamic memory allocation
Tutorial on principles and applications of database system (054) -- MySQL query (16): usage of date time function
How accurate is the IP address? What are dynamic IP and static IP? The most common method of switching IP
U++ learning notes ustruct, uenum declaration and function library simple function implementation
[secsha concept] large and small end
Nodejs builds cloud native microservice applications based on dapr, a quick start guide from 0 to 1
C语言进阶(一)动态分配内存
[go] how to control the maximum number of concurrent processes









