Weekend Trip Plan
InterviewTime limit1sMemory limit512 MB
Given a weighted graph and separate destination and lodging values at each location, find the maximum of w_a + w_b - dist(a, b) over all destination-lodging pairs.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Brute force, Implementation
- Solved
- No attempts yet
Problem
Youngseon wants to visit exactly one travel destination and one lodging over the weekend.
The region has key locations connected by bidirectional roads, each with a travel distance. Every location is reachable from every other location by road. Each destination and each lodging sits at one location, and no destination shares a location with a lodging.
Let be the expectation value of destination , the expectation value of lodging , and the shortest road distance between them. The score of a plan is
Choose the pair with the largest score and compute that maximum. The route between the two locations may pass through other destinations or lodgings.
Input
The first line contains the number of locations ().
The next lines give an adjacency matrix. The -th number on the -th line, , is the road distance between location and location . A value of means there is no direct road (, , ).
The next line contains the number of destinations and the number of lodgings (, ).
The next lines each give a destination location number and its expectation value. The following lines each give a lodging location number and its expectation value. Location numbers are between and inclusive, and every expectation value is between and inclusive.
Output
Print the maximum value of over all destination and lodging pairs. If every pair scores a negative value, print the largest (closest to zero) of them.