AcWing 1129. 热浪
原题链接
简单
作者:
navystar
,
2023-05-31 22:39:20
,
所有人可见
,
阅读 131
dijkstra
C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 2510;
bool st[N];
int n, m, s, e;
int g[N][N];
int dist[N];
inline int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
for (int i = 1; i < n; i ++ )
{
int t = -1;
for (int j = 1; j <= n; j ++ )
if (!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
st[t] = true;
for (int j = 1; j <= n ; j ++ )
if (dist[j] > dist[t] + g[t][j])
dist[j] = dist[t] + g[t][j];
}
return dist[e];
}
inline void solve()
{
memset(g, 0x3f, sizeof g);
cin >> n >> m >> s >> e;
while (m -- )
{
int a, b, c;
cin >> a >> b >> c;
int cnt = c < g[a][b] ? c : g[a][b];
g[a][b] = g[b][a] = cnt;
}
cout << dijkstra() << endl;
}
int main()
{
cin.tie(nullptr) -> sync_with_stdio(0);
solve();
return 0;
}