Find the minimum number of planes needed to operate all scheduled flights when planes can take repositioning flights between them.
Medium7GraphShortest pathNo attempts yetTime limit3sMemory limit256 MBAn airline operates flights out of n airports numbered 1 to n. The flight time from airport i to airport j is tij, and tij can differ from tji because of wind and terrain.
When a plane lands at airport i, it must be inspected for pi before it can take off again. The inspection time depends only on the airport where the plane lands, not on where the plane came from. A plane that lands on an extra repositioning flight is inspected the same way.
The airline must operate all m scheduled flights. Scheduled flight k leaves airport sk at exactly time tk and flies straight to airport fk. The airline may add any number of repositioning flights to move planes around, and one plane may make several of them in a row.
A plane that operated scheduled flight a can operate scheduled flight b next only if it is back at airport sb with its inspection finished by time tb. One plane cannot operate two scheduled flights that leave at the same time.
Stated exactly: the plane that finishes flight a is ready to take off again at Aa=ta+tsafa+pfa. Let dxy be the smallest time needed to move a plane from airport x to airport y, which is the minimum, over all routes, of the flight times along the route plus the inspection time at every airport where the plane lands, with dxx=0. One plane can operate b after a exactly when ta<tb and Aa+dfasb≤tb.
Find the minimum number of planes needed to operate all m scheduled flights.
The first line contains two space separated integers n and m. (1≤n,m≤500)
The second line contains n space separated integers p1,…,pn. (0≤pi≤106)
Each of the next n lines contains n space separated integers. The jth integer of the ith line is tij. (0≤tij≤106) It is guaranteed that tii=0 for every i, but tij and tji can differ when i=j.
Each of the next m lines contains three space separated integers si, fi, and ti. (1≤si,fi≤n, si=fi, 1≤ti≤106) It means the airline must operate a scheduled flight that leaves airport si at time ti and flies straight to airport fi.
Print, on a single line, the minimum number of planes needed to operate all m scheduled flights.