spfa朝花夕拾
作者:
jy9
,
2024-10-24 20:44:13
,
所有人可见
,
阅读 3
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1e5+10;
struct node{
int u, v;
};
int n, m;
int a, b, c;
int dist[N];
bool st[N];
vector<node> h[N];
void spfa(){
memset(dist, 0x3f, sizeof dist);
queue<int> q;
q.push(1);
dist[1] = 0;
st[1] = 1;
while(q.size()){
int t = q.front();
q.pop();
st[t] = 0;
for(auto [u, v]:h[t]){
if(dist[u] > dist[t] + v){
dist[u] = dist[t] + v;
if(st[u])continue;
q.push(u);
st[u] = 1;
}
}
}
}
int main(){
cin >> n >> m;
for (int i = 1; i <= m; i ++ ){
cin >> a >> b >> c;
h[a].push_back({b, c});
}
spfa();
if(dist[n] == 0x3f3f3f3f) cout << "impossible";
else cout << dist[n];
return 0;
}