PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000453. Mua một chuyến, miễn phí các chuyến còn lại (Buy one, get the rest free)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Mua một chuyến, miễn phí các chuyến còn lại (Buy one, get the rest free)

Buy one, get the rest free

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

GetsJo rents entire flights. Paying for a flight priced PP also makes all flights costing at most PP available at no additional charge. Each flight has an origin, destination, capacity, price and departure day. All contestants must reach city nn by the end of day dd.

Day 0 is today. A flight leaves city uu in the evening of day ee and arrives at city vv the following morning. Contestants can wait in any city, take a connecting flight on the evening of arrival, and follow different routes. Each flight carries at most its stated capacity. Contestants already in city nn may stay there. Find the minimum total rental cost under the promotion, or report that no feasible plan exists.

Input

The first line contains n,d,mn,d,m (cities, days, flights). Each of the next mm lines contains five integers u,v,c,p,eu,v,c,p,e: origin, destination, capacity, rental price and departure day. The last line contains nn integers z1,…,znz_1,\ldots,z_n, the initial contestant counts.

Output

Print Case #1: followed by the minimum integer cost, or Impossible if not all contestants can reach the destination. If no flight is needed, the cost is 0.

Subtasks

  • Subtask 1 (20 points): 1≤n≤51\le n\le5, 1≤d≤31\le d\le3, 0≤m≤200\le m\le20, 1≤u,v≤n1\le u,v\le n, 1≤c≤1001\le c\le100, 0≤p≤1000000\le p\le100000, 0≤e<d0\le e<d, 0≤zi≤1000\le z_i\le100, 1≤i≤n1\le i\le n; no two flights share the same (u,v,e)(u,v,e).
  • Subtask 2 (30 points): 1≤n≤151\le n\le15, 1≤d≤61\le d\le6, 0≤m≤2000\le m\le200, 1≤u,v≤n1\le u,v\le n, 1≤c≤1001\le c\le100, 0≤p≤1000000\le p\le100000, 0≤e<d0\le e<d, 0≤zi≤1000\le z_i\le100, 1≤i≤n1\le i\le n; no two flights share the same (u,v,e)(u,v,e).
  • Subtask 3 (50 points): 1≤n≤301\le n\le30, 1≤d≤101\le d\le10, 0≤m≤10000\le m\le1000, 1≤u,v≤n1\le u,v\le n, 1≤c≤1001\le c\le100, 0≤p≤1000000\le p\le100000, 0≤e<d0\le e<d, 0≤zi≤1000\le z_i\le100, 1≤i≤n1\le i\le n; no two flights share the same (u,v,e)(u,v,e).

Examples

Input 1

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

Output 1

Case #1: 30000

Explanation 1

The contestant in city 1 needs the flight costing 30000. Two groups of ten from city 2 reach city 4 on days 1 and 2; all 25 there fly to city 5 on day 2 and arrive on day 3. The 100 already at the destination stay. Cost 30000 is necessary and sufficient.

Input 2

2 1 1
1 2 99 10400 0
100 0

Output 2

Case #1: Impossible

Explanation 2

There are only 99 seats for 100 contestants who must travel from city 1 to city 2 in one day.