logo AlgoBeat OnlineJudge
登录 注册

#215772. [JAG 2023 Summer Camp #2] Drifting

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are given a weighted directed graph of vertices and edges, with vertices numbered to and edges numbered to . The -th () edge connects from vertex to vertex (), and the weight of the edge is .

Also, triplets of integers are given. The -th () triplet is ().

You start at vertex and move to vertex by repeatedly moving along an edge.

In addition, for all (), if you move from vertex to vertex directly, we must next move to a vertex other than vertex .

Judge whether it is possible to reach vertex . If it is possible to reach, also calculate the minimum sum of the weights of the edges you pass through.

输入格式

The input satisfies the following constraints.

  • All inputs consist of integers.

输出格式

If you cannot reach vertex , output . Otherwise, output the minimum sum of the weights of the edges you pass through.

样例

样例输入 1

4 4
1 2 1
1 3 2
2 4 2
3 4 2
1
1 2 4

样例输出 1

4

样例输入 2

7 8
1 2 5
1 3 2
2 4 1
3 4 1
4 5 6
4 6 2
5 7 1
6 7 1
2
2 4 5
3 4 6

样例输出 2

9

样例输入 3

3 2
1 2 1
2 3 1
1
1 2 3

样例输出 3

-1

数据范围与提示

In Sample Input 1, the best move is .

In Sample Input 2, the best move is .