#include<bits/stdc++.h> usingnamespace std; constint N = 1e6 + 10, M = N * 2; typedef pair<int, int> PII; int h[N], e[N], ne[N], w[N], idx; int dist[N]; bool st[N]; int n, m;
voidadd(int a, int b, int c) { e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx ++; }
intdj() { memset(dist, 0x3f, sizeof dist); dist[1] = 0; priority_queue<PII, vector<PII>, greater<PII>> heap; heap.push({0,1}); while(heap.size()) { PII t = heap.top(); heap.pop(); int ver = t.second, distance = t.first; if(st[ver])continue; st[ver] = 1; for(int i = h[ver]; i != -1; i = ne[i]) { int j = e[i]; if(dist[j] > distance + w[i]) { dist[j] = distance + w[i]; heap.push({dist[j], j}); } } } if(dist[n] == 0x3f3f3f3f)return-1; return dist[n]; }
intmain() { memset(h, -1, sizeof h); cin >> n >> m; while(m --) { int a, b, c; cin >> a >> b >> c; add(a, b, c); } int t = dj(); cout << t <<endl; return0; }
#include<bits/stdc++.h> usingnamespace std; constint N = 210; int g[N][N]; int n, m, t; voidfloyd() { for(int k = 1; k <= n; k ++) for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); } intmain() { cin >> n >> m >> t; memset(g, 0x3f, sizeof g); for(int i = 1; i <= n; i ++)g[i][i] = 0; for(int i = 1; i <= m; i ++) { int a, b, c; cin >> a >> b >> c; g[a][b] = min(g[a][b], c); } floyd(); while(t --) { int a, b; cin >> a >> b; if(g[a][b] > 1e9 / 2)puts("impossible"); else cout << g[a][b] << endl; } }