Mixed Flight Plans
Time limit1sMemory limit128 MB
Compute the cheapest cost to send two bags with direct and fixed-price indirect flights, either separately or by swapping bags at a shared airport.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Brute force
- Solved
- No attempts yet
Problem
Ad Hoc Postal Company (AHPC) delivers postal envelopes in a simple way. It advertises for a volunteer, buys that volunteer the cheapest flight plan from the source city to the destination city, and hands over a bag of envelopes at the source airport. The volunteer gives the bag to the company's correspondent at the destination airport.
Besides direct flights between two airports, there are indirect trips that stop at several airports in a fixed order. A passenger who buys an indirect trip has to board at its first airport and passes the airports one after another in the listed order, with no other direct flight or indirect trip in between. The passenger may give up the rest of the trip and get off at any intermediate airport. The price of an indirect trip is fixed, so it costs the same whether the passenger takes every flight of it or stops in the middle. A flight plan is built from direct flights and indirect trips (whole or partial), and its price is the sum of the prices of everything bought for it.
Two bags have to be sent, one from airport to and the other from to , where , , , are four different airports. The company picks one of these two arrangements.
- Buy a plan from to for the first volunteer and a plan from to for the second one.
- Mix the two plans to save money. Buy a plan from to for the first volunteer and a plan from to for the second one. If both plans stop at the same airport , the two volunteers meet at and exchange their bags, so the first one carries the second bag to and the second one carries the first bag to .
may be one of , , , , and it may also be an airport that a volunteer only passes while riding an indirect trip, without getting off there. The first volunteer has to pass before arriving at , and the second one has to pass before arriving at . The total price is the price of the first plan plus the price of the second plan.

The figure above shows six airports with direct flights (solid arrows), indirect trips (dashed and dotted arrows), and their prices. One bag goes from airport 3 () to 5 (), the other from 6 () to 1 (). If the company buys (3,4) and (4,5) for the first volunteer and (6,5) and (5,1) for the second one, the total price is 300. A cheaper choice is (3,4,1,2,6) for the first volunteer and (6,2) with (2,4,5) for the second one, for a total of 250. The volunteers meet at airport 4 () and swap bags there, and the first volunteer leaves the indirect trip at airport 1.
Given the flight information, write a program that finds the cheapest cost of delivering the two bags.
Input
The input has several test cases. The first line of each case has six integers , , , , , . () is the number of airports, numbered 1 through . () is the number of direct flights and indirect trips together.
The th of the next lines starts with two positive integers and . () is the price, and is 1 for a direct flight and 2 or more for an indirect trip. Then come distinct airport numbers in the order they are visited.
The line after the last case is 0 0 0 0 0 0, which must not be processed.
Output
For each test case, print the cheapest delivery cost on one line. If neither arrangement above can deliver both bags, print Impossible! without the quotes.