#P1807. 最长路

    ID: 761 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>图论NOI 导刊广度优先搜索,BFS拓扑排序

最长路

Description

Let GG be a weighted directed acyclic graph with nn vertices, labeled from 11 to nn. Design an algorithm to compute the longest path from 11 to nn in GG.

Input Format

The first line contains two integers, the number of vertices nn and the number of edges mm.

Lines 22 to (m+1)(m + 1) each contain three integers u,v,wu, v, w (u<vu<v), indicating there is a directed edge from uu to vv with weight ww.

Output Format

Output a single integer on one line, representing the length of the longest path from 11 to nn.

If 11 cannot reach nn, output 1-1.

2 1
1 2 1
1

Hint

Constraints

  • For 20%20\% of the testdata, n100n \leq 100, m103m \leq 10^3.
  • For 40%40\% of the testdata, n103n \leq 10^3, m104m \leq 10^{4}.
  • For 100%100\% of the testdata, 1n15001 \leq n \leq 1500, 0m5×1040 \leq m \leq 5 \times 10^4, 1u,vn1 \leq u, v \leq n, 105w105-10^5 \leq w \leq 10^5.

Translated by ChatGPT 5