Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
//	"strings"
    "sort"
//    "container/heap"
)

/*********** Input **************/
/*
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}*/

func scanInt(sc *bufio.Scanner) int {
    sc.Scan()
    v, _ := strconv.Atoi(sc.Text())
    return v
}
/**************  End Input  **************/

func toInt(str string) int {
	n, _ := strconv.Atoi(str)
	return n
}

type Edge struct {
	to      int
	newTime int
}

type State struct {
	node int
	time int
}


/**************  Cell  *****************/

type Cell struct {
	item *State
	next *Cell
}

func NewCell(x *State, cp *Cell) *Cell {
	newCp := new(Cell)
	newCp.item = x
	newCp.next = cp
	return newCp
}

/************  End Cell  ***************/

/**************  Queue  ****************/

type Queue struct {
	size  int
	front *Cell
	rear  *Cell
}

func NewQue() *Queue {
	return new(Queue)
}

func (q *Queue) Push(x *State) {
	newNode := NewCell(x, nil)
	if q.size == 0 {
		q.front = newNode
		q.rear = newNode
	} else {
		q.rear.next = newNode
		q.rear = newNode
	}
	q.size++
}

func (q *Queue) PopLeft() (*State, bool) {
	if q.size == 0 {
		return nil, false
	} else {
		x := q.front.item
		q.front = q.front.next
		q.size--
		if q.size == 0 {
			q.rear = nil
		}
		return x, true
	}
}

func (q *Queue) IsEmpty() bool {
	return q.size == 0
}

/***********  end Queue  ***********/

type EdgeSorter struct {
    edges []Edge
    lessfunc []lessFunc
    used int
    length int
}

type lessFunc func(i, j *Edge)bool

func (ES EdgeSorter) Len() int {
    return len(ES.edges)
}

func (ES EdgeSorter) Swap(i, j int) {
    ES.edges[i], ES.edges[j] = ES.edges[j], ES.edges[i]
}

func (ES EdgeSorter) Less(i, j int) bool {
    var k int
    p, q := &ES.edges[i], &ES.edges[j]
    for k=0; k<len(ES.lessfunc)-1;k++ {
        less := ES.lessfunc[k]
        switch {
            case less(p, q):
                return true
            case less(q, p):
                return false
        }
    }
    return ES.lessfunc[k](p, q)
}

/*********** PriorityQueue ***********/
/*
type PriorityQueue struct {
    arr []*State
    size int
}

func (pq PriorityQueue) Len() int {
    return len(pq.arr)
}

func (pq PriorityQueue) Swap(i, j int) {
    pq.arr[i], pq.arr[j] = pq.arr[j], pq.arr[i]
}

func (pq PriorityQueue) Less(i, j int) bool{
    return pq.arr[i].time < pq.arr[j].time
}

func (pq *PriorityQueue) Push(x interface{}) {
    item := x.(*State)
    pq.arr = append(pq.arr, item)
    pq.size++
}

func (pq *PriorityQueue) Pop() interface{} {
    n := pq.size
    item := pq.arr[n - 1]
    pq.arr = pq.arr[:n - 1]
    pq.size--
    return item
}
*/


func main() {
    sc := bufio.NewScanner(os.Stdin)
    sc.Split(bufio.ScanWords)
	n, m := scanInt(sc), scanInt(sc)
	edges := make([]EdgeSorter, 0)
	for i := 0; i < n; i++ {
		edges = append(edges, EdgeSorter{})
	}
	for i := 0; i < m; i++ {
        a, b, c := scanInt(sc) - 1, scanInt(sc) - 1, scanInt(sc)
		edges[a].edges = append(edges[a].edges, Edge{to: b, newTime: c})
	}

    byNewTime := func(p1 *Edge, p2 *Edge) bool{
        return p1.newTime >= p2.newTime
    }

    for i, _ := range edges {
        v := &edges[i]
        v.lessfunc = append(v.lessfunc, byNewTime)
        sort.Sort(v)
        v.used = 0
        v.length = len(v.edges)
    }
    var minTime [100000]int
    for i := 0; i < n; i++ {
        minTime[i] = 2000000000
    }
    minTime[0] = 0
    que := Queue{}
    que.Push(&State{node: 0, time: 0})
	ans := -1
	for !que.IsEmpty() {
		state, _ := que.PopLeft()
		for edges[state.node].used < edges[state.node].length {
            e := edges[state.node].edges[edges[state.node].used]
			if e.newTime >= state.time {
                if e.to == n - 1 && e.newTime > ans {
                    ans = e.newTime
             

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
//#define INF (1LL<<59)
  
struct edge{int to,cost;};
  
#define MAX_V 100000
vector<edge> G[MAX_V];
  
  
void dijkstra(int s, vector<int> &d){
    priority_queue< pii,vector<pii>,greater<pii> > que;
    rep( i,d.size() )d[i]=INF;
    d[s] = 0;
    que.push( pii(0,s) );
      
    while( que.size() ){
        pii p=que.top();
        que.pop();
          
        int v=p.second;
        if( d[v] < p.first )continue;
        rep(i,G[v].size()){
            edge e=G[v][i];
            if(d[e.to]>e.cost && d[v] <= e.cost ){
                d[e.to]=e.cost;
                que.push(pii(d[e.to],e.to));
            }
        }
    }
}
  
  
int main(){
    int n,m;
    cin>>n>>m;
      
    rep(i,m){
        int a,b,c;
        cin>>a>>b>>c;
        a--,b--;
        G[a].pb(edge{b,c});
    }
      
    vector<int> d(n);
    dijkstra(0,d);
      
      
    int ans = -1;
      
    rep(i,n){
        for(auto &e:G[i]){
            if(e.to==n-1){
                if(d[i]>e.cost)continue;
                ans = max(e.cost,ans);
            }
        }
    }
      
    cout<<ans<<endl;
      
      
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,b) FOR(i,0,b)
#define PB push_back
#define ALL(c) c.begin(),c.end()
using namespace std;
typedef long long LL;
typedef int ut;
typedef pair<ut,ut> pr;
typedef vector<ut> VI;
typedef vector<pr> Vpr;
typedef priority_queue<pr,Vpr,greater<pr> > PQ;
const int SIZE=2*1e5;
const ut INF=1<<30;
Vpr edge[SIZE];
int N,M,a,b,c;
int reach[SIZE];
int dijkstra(){
	PQ qu;
	qu.push(pr(0,1));
	while(!qu.empty()){
		int now=qu.top().second,times=qu.top().first;
		qu.pop();
	//	cout << now <<" " << times << endl;
		if(reach[now]!=times) continue;
		REP(i,edge[now].size()){
			int next=edge[now][i].first;
			int nextT=edge[now][i].second;
			if(nextT<times) continue;
			if(next==N){
				reach[next]=max(reach[next],nextT);
				continue;
			}
			if(nextT>=reach[next]) continue;
			
			reach[next]=nextT;
			qu.push(pr(nextT,next));
		}
	}
	return reach[N];
}
int main(){
	cin.tie(0);
	cin >> N >> M;
	FOR(i,1,N+1){
		reach[i]=INF;
	}
	reach[N]=-1;
	reach[1]=0;
	REP(i,M){
		cin >> a >> b >> c;
		edge[a].PB(pr(b,c));
		edge[b].PB(pr(a,c));
	}
	cout << dijkstra() << endl;
	return 0;
}

C++: #include <iostream>
#include <vector>

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define s second
#define f first
using namespace std;

vector<vector<pair<int, int> > > e;

int dfs(int time,int edge){
  int ret = time;
  if(edge == 0)return -2;
  //cout<<time<<" "<<edge<<endl;
  int cnt = 0;
  rep(i,e[edge].size()){
    pair<int,int> p = e[edge][i];
    cout<<edge<<" "<<p.f<<" "<<p.s<<endl;
    if(time >= p.second){ // change next edge
      ret = min(dfs(p.s,p.f),ret);
      cnt ++;
    }
  }
  if(cnt == 0)return -1;
  //cout<<" "<<ret<<" "<<edge<<endl;
  return ret;
}

int main(void){
  int n,m;
  cin>>n>>m;
  e = vector<vector<pair<int,int> > >(n);
  int a,b,c;
  rep(i,m){
    cin>>a>>b>>c;
    --a,--b;
    e[b].push_back(make_pair(a,c));
  }

  int ret = -1;
  int i = n-1;
  rep(j,e[i].size()){
    pair<int,int> eg = e[i][j];
    //cout<<eg.s<<" "<<eg.f<<endl;
    if(dfs(eg.s,eg.f) == -1)continue;
    ret = max(ret,eg.s);
  }
  cout<<ret<<endl;
}