Economic One-way Roads
Time limit5sMemory limit1024 MB
Given N up to 18 cities with a directed cost for each road direction, orient every road so all cities are mutually reachable at minimum total cost, or report -1.
- Level
Hard8 of 10
- Topics
- Graph, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
The country of RUN has cities, some of which are connected by two-way roads. Each road connects two different cities, and no two roads connect the same pair of cities. It is not guaranteed that every city is reachable from every other city by traveling along some roads.
Due to traffic issues, the mayor of RUN decided to make all roads one-way. After doing so, it must be possible to move from any city to any other city using one or more roads. To save as much money as possible, the mayor will pick the cheapest orientation among all orientations that satisfy this condition. The cost of orienting a road depends on both the road itself and the direction it is oriented in.
Input
The first line contains the number of cities ().
Each of the next lines contains space-separated integers. The -th integer in the -th line, , is the cost of orienting the road from city to city , or if no road connects these two cities.
For all , . For all distinct and , either , or .
Output
Print the minimum cost needed to orient all roads so that the condition is satisfied. If no valid orientation exists, print .