朴素SPFA-注意st数组
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5 + 10;
int e[N], h[N], ne[N], idx, w[N];
int q[N], dist[N];
int hh = 0, tt = -1;
int n, m;
bool st[N];
void add(int a, int b, int c) {
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx ++ ;
}
int main() {
memset(h, -1, sizeof h);
memset(dist, 0x3f, sizeof dist);
cin >> n >> m;
for (int i = 0; i < m; i ++ ) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
dist[1] = 0;
q[++ tt] = 1;
st[1] = true;
while (hh <= tt) {
auto t = q[hh ++ ];
st[t] = false;
for (int i = h[t]; i != -1; i = ne[i]) {
int j = e[i], we = w[i];
if (dist[j] > dist[t] + we) {
dist[j] = dist[t] + we;
if (!st[j]) {
q[++ tt] = j;
st[j] = true;
}
}
}
}
if (dist[n] == 0x3f3f3f3f) cout << "impossible" << endl;
else cout << dist[n] << endl;
return 0;
}