#P8724. [蓝桥杯 2020 省 AB3] 限高杆
[蓝桥杯 2020 省 AB3] 限高杆
Problem Description
A certain city has intersections and 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, and , located near intersections and respectively. A truck departs from market , first goes to intersection , then travels through the road system to reach intersection , and only then can it arrive at market . 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 and . What is the maximum distance that can be reduced?
Input Format
The first line contains two integers and , representing the number of intersections and the number of road segments.
The next lines each contain four integers , , , , indicating that there is a road segment of length between intersections and . If is , it means there is no height-limit bar on this road segment; if is , 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 to intersection .
Output Format
Output one line containing one integer, which is the maximum distance by which the route between markets and 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 to changes from to , a reduction of .
Constraints and Conventions
For of the testdata, , , .
For of the testdata, , , .
For of the testdata, , , .
For all testdata, , , , 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