UVa 10983 - Buy one, get the rest free

10983 - Buy one, get the rest free

 * http://acm.uva.es/p/v109/10983.html

Summary
There are $$n$$ cities numbered from 1 to $$n$$ and $$m$$ flights. For each flight, you know its departure and arrival cities and days, cost of ticket and capacity (number of people it carries). A certain number of people from each city needs to be gather in the $$n$$-th city in $$d$$ days. What's the least maximum cost of a ticket they have to buy?

Explanation
Do a binary search on the maximum cost of ticket. For a fixed cost $$C$$, construct a directed graph, whose vertices are pairs (city, day), and edges are flights of cost at most $$C$$. Edge's capicity equals corresponding flight's capacity. Add a vertex $$s$$, connected to every vertex $$(i,0)$$ with an edge of capacity $$z_i$$, and find the maximum flow between vertices s and $$(n,d)$$. It's value is equal to the sum of all $$z_i$$ if and only if there is a way for all people to fly to $$n$$-th city in $$d$$ days.

Input
2 5 4 5 1 5 100 30000 0 2 4 10 10000 0 2 4 10 10000 1 4 5 25 25000 2 2 5 100 40000 3 1 20 0 5 100 2 1 1 1 2 99 10400 0 100 0

Output
Case #1: 30000 Case #2: Impossible