Of the Children
Time limit1sMemory limit1024 MB
Given per-city donations and travel costs between cities, find the least initial money at city 0 so the traveler can reach city N-1 without running short on any leg.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Note
This problem was withdrawn from the contest by a minute-1 clarification message sent to all teams.
The reason for the withdrawal was that, as of 24 hours before the contest, the authoring team had only a single sample solution, and we strongly prefer to move forward with a problem only after independent confirmation by two or more authors.
Problem
A nation-wide charity, Won't Someone Think of the Children, is sending one of its celebrity spokespeople on a coast-to-coast publicity tour. Like many charities, it is short on funds and must plan carefully to pay the celebrity's travel expenses from its office on the west coast to the final press event on the east coast.
Local offices of the charity have been taking pledges and collecting donations to pay for the celebrity to visit their cities as part of the tour. The national office has made no promises that the celebrity will visit any of these intermediate locations, but hopes that these locally collected funds can help fund the coast-to-coast trip. Without the help of the local offices, the national office is not sure it can afford to get the spokesperson to the final destination.
The trip is paid for incrementally. The celebrity can travel from one city to another only if he or she has enough money to pay for the transportation to that next city. Upon arrival at a city, the celebrity collects any money held there and uses it to pay for the later legs of the journey. The celebrity can visit a given city only once.
Given the cities that have invited the celebrity, the amount of money raised by each city, and the travel costs between pairs of cities, what is the smallest amount of money the home office must provide at the beginning of the journey so that the celebrity can reach the end without being unable to pay for any leg of the trip?
Input
Input consists of one or more datasets. Each dataset begins with a line containing a single integer , , the number of cities. A value of 0 for marks the end of input.
Cities are numbered . City 0 is the home office and the starting point. City is the final destination.
The next lines each contain an integer in the range , the money collected by cities . The money at city is irrelevant, and the amount to be provided at city 0 is what you compute.
The following lines each contain three integers , , and , where and are distinct cities in the range and is the travel cost between them, with . The cost is the same in both directions. There is at least 1 and at most such line. The list ends with a line where , , and are all negative.
Output
For each dataset, print a single line.
If city can be reached from city 0, print the smallest amount of money the celebrity must be given at city 0 to guarantee reaching city .
If city cannot be reached from city 0, print .