2022-10-28 13:33:57 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <queue>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2021-08-11 12:52:48 +00:00
|
|
|
|
2022-10-28 13:33:57 +00:00
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
const char endl = '\n';
|
2021-08-11 12:52:48 +00:00
|
|
|
|
2022-10-28 13:33:57 +00:00
|
|
|
const int N = 1e5 + 5;
|
|
|
|
const int INF = 0x3f3f3f3f;
|
|
|
|
|
|
|
|
int n, m, dist[N];
|
|
|
|
std::vector<std::pair<int, int>> g[N];
|
|
|
|
bool vis[N];
|
2021-08-11 12:52:48 +00:00
|
|
|
|
|
|
|
void spfa() {
|
2022-10-28 13:33:57 +00:00
|
|
|
std::fill_n(dist, N, INF);
|
|
|
|
|
|
|
|
std::queue<int> q;
|
|
|
|
|
|
|
|
q.emplace(1);
|
2021-08-11 12:52:48 +00:00
|
|
|
dist[1] = 0;
|
2022-10-28 13:33:57 +00:00
|
|
|
vis[1] = true;
|
|
|
|
|
2021-08-11 12:52:48 +00:00
|
|
|
while (!q.empty()) {
|
2022-10-28 13:33:57 +00:00
|
|
|
int u = q.front();
|
2021-08-11 12:52:48 +00:00
|
|
|
q.pop();
|
2022-10-28 13:33:57 +00:00
|
|
|
|
|
|
|
vis[u] = false;
|
|
|
|
|
|
|
|
for (auto e : g[u]) {
|
|
|
|
int v = e.first,
|
|
|
|
w = e.second;
|
|
|
|
|
|
|
|
if (dist[v] > dist[u] + w) {
|
|
|
|
dist[v] = dist[u] + w;
|
|
|
|
|
|
|
|
if (!vis[v]) {
|
|
|
|
vis[v] = true;
|
|
|
|
q.emplace(v);
|
|
|
|
}
|
2021-08-11 12:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2022-10-28 13:33:57 +00:00
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
cin.tie(nullptr);
|
|
|
|
|
2021-08-11 12:52:48 +00:00
|
|
|
cin >> n >> m;
|
2022-10-28 13:33:57 +00:00
|
|
|
|
|
|
|
for (int i = 1, u, v, w; i <= m; i++) {
|
2021-08-11 12:52:48 +00:00
|
|
|
cin >> u >> v >> w;
|
2022-10-28 13:33:57 +00:00
|
|
|
|
|
|
|
g[u].emplace_back(v, w);
|
2021-08-11 12:52:48 +00:00
|
|
|
}
|
2022-10-28 13:33:57 +00:00
|
|
|
|
2021-08-11 12:52:48 +00:00
|
|
|
spfa();
|
2022-10-28 13:33:57 +00:00
|
|
|
|
|
|
|
if (dist[n] == INF) {
|
2021-08-11 12:52:48 +00:00
|
|
|
cout << "impossible" << endl;
|
|
|
|
} else {
|
|
|
|
cout << dist[n] << endl;
|
|
|
|
}
|
2022-10-28 13:33:57 +00:00
|
|
|
|
2021-08-11 12:52:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|