0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

849. Dijkstra求最短路 I

https://www.acwing.com/problem/content/submission/code_detail/6501777/
This commit is contained in:
Baoshuo Ren 2021-07-17 20:57:19 +08:00 committed by Baoshuo Ren
parent 7741c40d36
commit 9764800d02
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

39
AcWing/849/849.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, z, g[505][505], dist[505];
bool st[505];
int diskstra() {
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;
}
return dist[n] == 0x3f3f3f3f ? -1 : dist[n];
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
g[i][j] = i == j ? 0 : 0x3f3f3f3f;
}
}
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
g[x][y] = min(g[x][y], z);
}
cout << diskstra() << endl;
return 0;
}