luogu#P4814. [CCO 2014] 国王格鲁夫

[CCO 2014] 国王格鲁夫

Problem Description

This problem is translated from CCO 2014 Day 1 T2 “King Gruff”.

Wolf King Gruff rules a prosperous and happy land inhabited by adorable foxes. Unfortunately for the foxes, he is not a good king at all, and he wants to make their lives miserable.

His country has NN cities connected by MM roads. Road ii allows you to travel from city XiX_i to another city YiY_i, and you may only travel in this direction. This road has length LiL_i and closing cost CiC_i. There may be multiple roads in the same direction connecting the same pair of cities.

King Gruff especially dislikes foxes living in two different cities AA and BB, and he wants traveling from city AA to city BB to be difficult or even impossible. Specifically, he will choose a distance DD and close some roads in his kingdom at the same time. A road must be closed if it is part of some path from city AA to city BB whose total length is at most DD. For each such road, he will pay its closing cost from the royal treasury. A path consists of a sequence of roads where, except for the first road, the start city of each road is the end city of the previous road. The same city or the same road may be visited multiple times.

Gruff cannot decide which DD to choose. In general, a larger DD can make life more inconvenient for his fox subjects, but it may cost him more money. Therefore, he has come up with QQ different values D1,D2,...,DQD_1, D_2, ..., D_Q. For each value, he wants to know how much it would cost to meet his requirement. Since you also dislike foxes, you agree to help him write a program to compute the minimum total cost.

Input Format

The first line contains four integers N,M,A,BN, M, A, B separated by spaces: the number of cities, the number of roads, the starting city, and the ending city.

The next MM lines each contain four integers Xi,Yi,Li,CiX_i, Y_i, L_i, C_i separated by spaces, describing a road from XiX_i to YiY_i with length LiL_i and closing cost CiC_i.

The next line contains an integer QQ, the number of queries.

The next QQ lines each contain one integer DiD_i, the distance parameter described above.

Output Format

Output QQ lines. Each line should be the total cost to close all required roads for the corresponding distance parameter DiD_i (1iQ1 \le i \le Q).

4 5 1 3
1 2 5 1
1 2 8 50
2 3 2 15
3 1 80 1000
3 4 1 1
4
8
6
90
94
16
0
66
1066
4 3 1 2
2 1 1 1
3 4 10000 10000
4 3 10000 10000
1
1000000000
0

Hint

The kingdom is shown in the figure below, where each road is labeled only with its length.

CCO2014D1T2Pic1

If D=8D = 8, the first and third roads must be closed. They are both part of a path from city 11 to city 33 of length 77, so the total cost is 1+15=161 + 15 = 16.

If D=6D = 6, no roads need to be closed, because there is no path from city 11 to city 33 with length less than or equal to 66.

If D=90D = 90, the first three roads must all be closed.

If D=94D = 94, the fourth road must also be closed, because it lies on a path from city 11 to city 33 whose length is exactly 9494. The path takes, in order, the first, third, fourth, first, and third roads.

The kingdom is shown in the figure below.

CCOD2T2Pic2

As you can see, the foxes are already in trouble, because there is no path from city 11 to city 22 at all. Therefore, for any DD, King Gruff does not need to close any roads.

Constraints:

For at most 20%20\% of the testdata, 1N5001 \le N \le 500.

For at most 20%20\% of the testdata, Q=1Q = 1.

For at most 80%80\% of the testdata, 1Q201 \le Q \le 20.

For 100%100\% of the testdata, 1Xi,Yi,A,BN1051 \le X_i, Y_i, A, B \le N \le 10^5, 0M1050 \le M \le 10^5, 1Li,Ci1041 \le L_i, C_i \le 10^4, 1Q1051 \le Q \le 10^5, 1D1091 \le D \le 10^9

Translated by ChatGPT 5