Skip to content

Instantly share code, notes, and snippets.

@yurahuna
Created September 22, 2016 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurahuna/890056d08b968e287877fc4ecae94520 to your computer and use it in GitHub Desktop.
Save yurahuna/890056d08b968e287877fc4ecae94520 to your computer and use it in GitHub Desktop.
struct edge {
int to, cost;
edge(){}
edge(int _to, int _cost) : to(_to), cost(_cost) {}
};
using Graph = vector<vector<edge>>;
vector<int> dijkstra(const Graph& G, int s, int g) {
int n = G.size();
priority_queue<Pii, vector<Pii>, greater<Pii>> pq; // cost, vertex
vi d(n, inf);
d[s] = 0;
pq.push(make_pair(0, s));
while (!pq.empty()) {
auto p = pq.top(); pq.pop();
int v = p.second;
if (v == g) break;
if (d[v] < p.first) continue;
for (const auto& e : G[v]) {
if (d[v] <= e.cost && d[e.to] > e.cost) {
d[e.to] = e.cost;
pq.push(make_pair(d[e.to], e.to));
}
}
}
return d;
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int N, M;
cin >> N >> M;
Graph G(N);
rep(i, M) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
G[a].emplace_back(b, c);
}
auto d = dijkstra(G, 0, -1);
int ans = -1;
rep(i, N) {
for (auto e : G[i]) {
if (e.to == N - 1 && d[i] <= e.cost) {
ans = max(ans, e.cost);
}
}
}
cout << ans << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment