#P6772. [NOI2020] 美食家

[NOI2020] 美食家

Problem Description

After the Elf Kingdom on the continent of Bzeroth repelled the invasion of the Catastrophe Legion, it spent more than ten years recovering and once again became a thriving paradise that attracts visitors from all directions. Little W is a famous gourmet who has traveled around the world, and he has now come to the Elf Kingdom by reputation.

The Elf Kingdom has nn cities, numbered from 11 to nn. The food in city ii can provide Little W with cic_i happiness. The cities are connected by mm directed roads, numbered from 11 to mm. For road ii, its start city is uiu_i, its end city is viv_i, and traveling along it takes wiw_i days. That is, if Little W travels from city uiu_i along road ii on day dd, then he will arrive at city viv_i on day d+wid + w_i.

Little W plans a trip in the Elf Kingdom lasting TT days. More specifically: he will set off from city 11 on day 00, and after TT days of travel, he will return to city 11 on exactly day TT to end the trip. Since Little W is a gourmet, every time he arrives at a city (including city 11 on day 00 and day TT), he will taste the food of that city and gain the happiness it provides. If Little W arrives at the same city multiple times, he will gain happiness multiple times. Note that during the trip, Little W cannot stay in any city. That is, when he arrives at a city and the trip has not ended yet, he must depart immediately on the same day to another city.

For the figure above, one feasible travel plan of length 1111 days is 1212311 \to 2 \to 1 \to 2 \to 3 \to 1:

  • On day 00, Little W starts the trip from city 11, gains happiness 11, and departs for city 22.
  • On day 11, Little W arrives at city 22, gains happiness 33, and departs for city 11.
  • On day 44, Little W arrives at city 11, gains happiness 11, and departs for city 22.
  • On day 55, Little W arrives at city 22, gains happiness 33, and departs for city 33.
  • On day 77, Little W arrives at city 33, gains happiness 44, and departs for city 11.
  • On day 1111, Little W arrives at city 11, gains happiness 11, and ends the trip.
  • The total happiness gained in this trip is 1313.

In addition, the Elf Kingdom will hold kk food festivals at different times. Specifically, the ii-th food festival will be held in city xix_i on day tit_i. If Little W is in city xix_i on day tit_i, then when he tastes the food in city xix_i, he will gain an extra yiy_i happiness. Now Little W asks you, as the reception envoy of the Elf Kingdom, to help him compute the maximum possible total happiness he can gain during the trip.

Input Format

Read data from standard input.

The first line contains four integers n,m,T,kn, m, T, k, representing the number of cities, the number of roads, the travel days, and the number of food festivals.

The second line contains nn integers cic_i, representing the happiness provided by the food in each city. Then follow mm lines, each containing three integers ui,vi,wiu_i, v_i, w_i, representing the start city, end city, and travel days of each road.

Finally, there are kk lines, each containing three integers ti,xi,yit_i, x_i, y_i, representing the time, city, and extra happiness provided by each food festival.

The testdata guarantees:

  • For all 1im1 \leq i \leq m, uiviu_i \neq v_i. However, the testdata may contain multiple directed roads with the same route, i.e. there may exist 1i<jm1 \leq i < j \leq m such that ui=uju_i = u_j and vi=vjv_i = v_j.
  • For every city, there exists at least one directed road starting from that city.
  • The times tit_i of all food festivals are pairwise distinct.

Output Format

Output to standard output.

Output a single integer in one line, representing the maximum total happiness Little W can obtain through traveling.

If Little W cannot return to city 11 on day TT, output 1-1.

3 4 11 0
1 3 4
1 2 1
2 1 3
2 3 2
3 1 4
13
4 8 16 3
3 1 2 4
1 2 1
1 3 1
1 3 2
3 4 3
2 3 2
3 2 1
4 2 1
4 1 5
3 3 5
1 2 5
5 4 20
39

Hint

Explanation for Sample 1

This sample is the example in the statement. The optimal travel plan is shown in the statement.

Explanation for Sample 2

The optimal plan is 13423411 \to 3 \to 4 \to 2 \to 3 \to 4 \to 1.

  • On day 00, Little W starts the trip from city 11, gains happiness 33, and travels along road 33.
  • On day 22, Little W arrives at city 33, gains happiness 22, and travels along road 44.
  • On day 55, Little W arrives at city 44, gains happiness 20+420 + 4 due to the food festival, and travels along road 77.
  • On day 66, Little W arrives at city 22, gains happiness 11, and travels along road 55.
  • On day 88, Little W arrives at city 33, gains happiness 22, and travels along road 44.
  • On day 1111, Little W arrives at city 44, gains happiness 44, and travels along road 88.
  • On day 1616, Little W arrives at city 11, gains happiness 33, and ends the trip.
  • The total happiness gained is 3939.

Sample 3

See delicacy/delicacy3.in and delicacy/delicacy3.ans in the contestant directory.

This sample satisfies k=0k=0.


Constraints

For all test points:

1n501 \leq n \leq 50, nm501n \leq m \leq 501, 0k2000 \leq k \leq 200, 1tiT1091 \leq t_i \leq T \leq 10^9.

1wi51 \leq w_i \leq 5, 1ci525011 \leq c_i \leq 52501, 1ui,vi,xin1 \leq u_i, v_i, x_i \leq n, 1yi1091 \leq y_i \leq 10^9.

The detailed limits for each test point are shown in the table below:

Test Point ID nn mm TT Special Constraints
141\sim 4 5\le 5 50\le 50 5\le 5 None
585\sim 8 50\le 50 52501\le 52501
9109\sim 10 109\le 10^9 A
111311\sim 13 k=0k=0
141514\sim 15 k10k \le 10
161716\sim 17 None
182018\sim 20 501\le 501

Special constraint A: n=mn = m and ui=i,vi=(imodn)+1u_i = i, v_i = (i \bmod n) + 1.

Translated by ChatGPT 5