luogu#P4802. [CCO 2015] 路短最

[CCO 2015] 路短最

Problem Description

This problem is translated from CCO 2015 Day 1 T2 “Artskjid.

You can use many algorithms to find the shortest path from one place to another. People install GPS devices in their cars, and then their phones tell them the fastest way to reach the destination. However, when on vacation, Troy likes to travel slowly. He wants to find the longest path to the destination so that he can see many new and interesting places along the way.

Therefore, a valid path consists of a sequence of distinct cities c1,c2,,ckc_1,c_2,\ldots,c_k, and for each 1i<k1\le i<k, there is a road from cic_i to ci+1c_{i+1}.

He does not want to visit any city more than once. Please help him find the longest path.

Input Format

The first line contains two integers n,mn,m, representing the number of cities and the number of roads between cities. There is at most one road between any pair of cities. Cities are numbered from 00 to n1n-1. Troy starts at city 00, and city n1n-1 is his destination.

The next mm lines each contain three integers s,d,ls,d,l. Each triple indicates that there is a directed road of length ll from city ss to city dd. Each road is one-way: you can only travel from ss to dd, not in the reverse direction. It is guaranteed that there is a path from city 00 to city n1n-1.

Output Format

Output one integer: the length of the longest path that starts at city 00 and ends at city n1n-1, without visiting any city more than once. The path length is the sum of the lengths of the roads used.

3 3
0 2 5
0 1 4
1 2 3
7

Hint

The shortest path is to take the road directly from city 00 to city 22, with length 55 km. The longest path is 0120 \to 1 \to 2, with length 4+3=74+3=7 km.

For at least 30%30\% of the testdata, n8n\le 8.

For 100%100\% of the testdata, 2n182\le n \le 18, 1mn2n1\le m \le n^2-n, 0s,dn10\le s,d \le n-1, sds\neq d, 1l100001\le l\le 10000.

Translated by ChatGPT 5