TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

You are given a directed graph with positive edge weights, a start point and an end point. Output the shortest path between the start point and the end point. Note that there may exist multiple edges in the graph, i.e. there may exist more than one edge from a vertex to another vertex. There may also exist self edges in the graph, i.e. edges with same incident vertices.

Input Format

There are several test cases. Each test case starts with a number n (1<=n<=5000) and a number e, denoting the number of vertices and the number of edges respectively. Following are e lines, each with three numbers x, y and w (1<=w<=1000), denoting an edge x->y and its weight w. Finally there are two numbers at the end of each test case, denoting the start point and the end point. The vertex number starts with 1.

Output Format

For each test case, output the shortest path weight on a line by itself. In case there’s no path from the start point to the end point, output -1.

Sample Input 1

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

Sample Output 1

3
2
-1

Hints

Problem Source

Migrated from old NTUJ.

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 5000 65536 20