spfa求最短路
#include <bits/stdc++.h>
using namespace std;const int N = 150010;
typedef pair<int,int> PII;
int e[N], h[N], ne[N], w[N], idx;
int n, m;
int dist[N];
int st[N];void add(int a, int b, int c){e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}// spfa里的st存储的是 队列里是否有这个元素
// dijkstra 算法里的st存的是这个点是否已经访问过, 已经是最小路径了
int spfa(){memset(dist, 0x3f, sizeof dist);dist[1] = 0;queue<int> q;q.push(1);while(q.size()){auto t = q.front();q.pop();st[t] = 0;for(int i = h[t]; i != - 1; i = ne[i]){int j = e[i];if(dist[j] > dist[t] + w[i]){dist[j] = dist[t] + w[i];if(!st[j]){q.push(j);st[j] = 1;}}}}if(dist[n] == 0x3f3f3f3f) return 0x3f3f3f3f;return dist[n];
}int main(){ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);memset(h, -1, sizeof h);cin >> n >> m;for(int i = 1; i <= m; i++){int a, b, c;cin >> a >> b >> c;add(a, b, c);}int ans = spfa();if(ans == 0x3f3f3f3f) puts("impossible");else cout << ans << "\n";return 0;
}