Created
September 22, 2016 05:36
-
-
Save yurahuna/890056d08b968e287877fc4ecae94520 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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