#P6759. [USACO06OPEN] 县集市 The County Fair

[USACO06OPEN] 县集市 The County Fair

Problem Description

Every year, FJ likes to go to the county fair. There are nn booths at the fair, and each booth ii will give out an exquisite gift at a specific time pip_i on that day. FJ has heard about this and hopes to collect as many gifts as possible to share with his cows. To get the gift from booth ii, FJ must make sure he is at booth ii exactly at time pip_i.

To collect as many gifts as possible, FJ did a detailed investigation. From this investigation, FJ determined the time ti,jt_{i,j} it takes to travel from booth ii to booth jj. The layout of the fair is very unusual, which means that if FJ chooses to detour via other booths while traveling from ii to jj, it might take less time than going directly from ii to jj. However, the straightforward FJ never does this: if he wants to go from booth ii to booth jj, he will definitely spend ti,jt_{i,j} time to walk from ii to jj. In addition, because the fairground is rugged, ti,jt_{i,j} may be different from tj,it_{j,i}.

FJ is at booth 11 at time 00. Please compute the maximum number of gifts he can collect.

Input Format

Line 11: An integer nn, the number of booths.

Lines 22 to n+1n+1: nn integers. The integer on line i+1i+1 is pip_i, the time when booth ii gives out its gift.

Lines n+2n+2 to n2+n+1n^2+n+1: n2n^2 lines. The first nn lines describe the time needed for FJ to walk from booth 11 to booths 11 to nn (t1,1,t1,2,,t1,nt_{1,1}, t_{1,2}, \ldots, t_{1,n}). The next nn lines describe the time needed for FJ to walk from booth 22 to booths 11 to nn (t2,1,t2,2,,t2,nt_{2,1}, t_{2,2}, \ldots, t_{2,n}), and so on. The last nn lines describe the time needed for FJ to walk from booth nn to booths 11 to nn (tn,1,tn,2,,tn,nt_{n,1}, t_{n,2}, \ldots, t_{n,n}). For any booth ii, ti,i=0t_{i,i}=0.

Output Format

One line: An integer, the maximum number of gifts FJ can collect.

4
13
9
19
3
0
10
20
3
4
0
11
2
1
15
0
12
5
5
13
0
3

Hint

Sample Explanation

In the sample, there are 44 booths at the fair. Booth 11 gives out gifts at time 1313, booth 22 at time 99, booth 33 at time 1919, and booth 44 at time 33.

FJ first walks from booth 11 to booth 44, which takes 33 time units, and arrives at time 33, so he can get the gift. Then he walks from booth 44 to booth 22, which takes 55 time units, and arrives at time 88. After waiting for 11 time unit, he can get the gift from booth 22. Next he walks from booth 22 to booth 11, which takes 44 time units, and arrives at time 1313, so he can collect the third gift.

Constraints

For 100%100\% of the testdata, 1n4001\le n\le 400, 0pi1090\le p_i\le 10^9, 1ti,j1061\le t_{i,j}\le 10^6.

The testdata is from darkbzoj.

Translated by ChatGPT 5