Moon Walk

0

0 votes
Bitmask, Medium, travelling-salesman, easy-medium, dp
Problem

This republic day Avinash heard about moon walk which by the way is a dance move but he interpreted in literal terms and thought of doing so.

There are N tourist spot on moon (all are numbered from 1 to N ). Avinash is at 1 tourist spot and wished to do a moon walk on all N tourist spots. Time taken to travel from ith to jth spot is given as dij (NOTE dij = dji may NOT hold)

Though there are Q restrictions that must be satisfied 

Tourist spot ui must be visited before vi.

Find the minimum time required to visit all the tourist spot. Last tourist spot can be anyone of them.

NOTE : All the tourist spot must be visited only once.


Input

First line of input contains the number of tourist spot N, from subsequent line there will be N lines each containing N values dij​​​​​​​. 

On the next line we have Q​​​​​​​, and subsequent Q​​​​​​​ lines will contain two values each having ui and vi​​​​​​​.

Output

Minimum time required to visit all the tourist spots.

Constraints

1N14

0dij107

dii=0

0QN(N1)/2

1ui,viN

uivi

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Possible walks will be 1 -> 2 - > 3 and 1 ->3 -> 2.

But with the given constraints that Avinash need to travel 2nd spot before 3rd spot so second possiblity of walking will not be considered.

1 ->2->3 = d12 + d23 =  16

Editor Image

?