//存在负权值 处理负环 //如果能求出来 一般是不存在负权回路 //如果有负回路 那最小距离可能是负无穷 #include <cstring> #include <iostream> using namespace std; const int N = 1e4+1; struct Edge { int a; int b; int w; } edge[N]; int n, m, k; int dist[N], backup[N]; void bellman_ford() { memset(dist, 0x3f, sizeof dist); dist[1] = 0; ////迭代k次,表示经过不超过k条边走到每个点的距离 for (int i=0; i<k; i++) { memcpy(backup , dist, sizeof dist);//备份,不加备份可能出现串联 for (int j=0; j<m; j++) { int a=edge[j].a,b=edge[j].b,w=edge[j].w; dist[b] = min(dist[b],backup[a]+w);//只用上一次的 } } if (dist[n] > 0x3f3f3f3f/2) cout << "impossible"; else cout << dist[n]; } int main() { cin >> n >> m >> k; for (int i=0; i<m; i++) { int a, b, w; cin >> a >> b >> w; edge[i] = {a, b, w}; } bellman_ford(); return 0; }