#P8724. [蓝桥杯 2020 省 AB3] 限高杆

[蓝桥杯 2020 省 AB3] 限高杆

Problem Description

A certain city has nn intersections and mm road segments connecting these intersections, forming the city's road system. Each road segment connects two different intersections. Roads do not pass through any intersection in the middle.

For various reasons, some road segments have height-limit bars installed in the middle. Trucks cannot pass through road segments that have height-limit bars.

There are two important markets, AA and BB, located near intersections 11 and nn respectively. A truck departs from market AA, first goes to intersection 11, then travels through the road system to reach intersection nn, and only then can it arrive at market BB. Both markets are very busy, and many trucks travel back and forth between them every day.

The mayor found that because there are many height-limit bars, trucks may need to take detours to travel between the markets. This makes the driving distance within the road system longer, increasing wear on the road system, increasing energy consumption, and also increasing environmental pollution.

The mayor decided to remove the height-limit bars on two road segments to shorten the distance between markets AA and BB. What is the maximum distance that can be reduced?

Input Format

The first line contains two integers nn and mm, representing the number of intersections and the number of road segments.

The next mm lines each contain four integers aa, bb, cc, dd, indicating that there is a road segment of length cc between intersections aa and bb. If dd is 00, it means there is no height-limit bar on this road segment; if dd is 11, it means there is a height-limit bar on this road segment. There may be multiple road segments between the same pair of intersections.

The input guarantees that without removing any height-limit bars, trucks can normally travel through the road system from intersection 11 to intersection nn.

Output Format

Output one line containing one integer, which is the maximum distance by which the route between markets AA and BB can be reduced after removing the height-limit bars on two road segments.

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

Hint

Sample Explanation

There are only two road segments with height-limit bars. After removing them all, the distance from 11 to nn changes from 1717 to 1111, a reduction of 66.

Constraints and Conventions

For 30%30\% of the testdata, 2n102 \leq n \leq 10, 1m201 \leq m \leq 20, 1c1001 \leq c \leq 100.

For 50%50\% of the testdata, 2n1002 \leq n \leq 100, 1m10001 \leq m \leq 1000, 1c10001 \leq c \leq 1000.

For 70%70\% of the testdata, 2n10002 \leq n \leq 1000, 1m100001 \leq m \leq 10000, 1c100001 \leq c \leq 10000.

For all testdata, 2n100002 \leq n \leq 10000, 2m1052 \leq m \leq 10^5, 1c100001 \leq c \leq 10000, and there are at least two road segments with height-limit bars.

Lanqiao Cup 2020, Third Round Provincial Contest, AB Group, Problem H.

Translated by ChatGPT 5