This page is still under construction.

Parts of this page are still being built. What you see may change.

Railway Connection

Time limit1sMemory limit128 MB

Summary
Find the cheapest route from station s to g in a multigraph where each maximal run of same-company edges is priced by that company's piecewise linear, concave fare table.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming, Implementation
Solved
No attempts yet

Problem

Tokyo has a very complex railway system. The figure below shows a partial map of its lines and stations.

A sample railway network.

Suppose you want to travel from station A to station D. The path with the shortest distance is clearly A → B → D. However, the shortest path is not necessarily the cheapest. Suppose lines A–B, B–C, and C–D are operated by one railway company, while line B–D is operated by another. Then the path A → B → C → D may cost less than A → B → D.

The reason is that fares are not proportional to distance: usually, the longer the distance, the lower the fare per unit distance. When you use lines from more than one company, the fares charged by each company are simply added together, so a shorter route that mixes companies may end up costing more.

Given a railway network with several companies and each company's fare table (the rule that turns a distance into a fare), together with a starting station and a goal station, write a program that computes the minimum possible total fare.

Input

The input consists of multiple datasets, each in the following format.

n m c s g
x_1 y_1 d_1 c_1
...
x_m y_m d_m c_m
p_1 ... p_c
q_1,1 ... q_1,(p_1 - 1)
r_1,1 ... r_1,p_1
...
q_c,1 ... q_c,(p_c - 1)
r_c,1 ... r_c,p_c

Every value is a non-negative integer, and values on the same line are separated by a single space.

The first line describes the network and the trip. nn is the number of stations (2≤n≤1002 \le n \le 100). mm is the number of lines connecting two stations (0≤m≤100000 \le m \le 10000). cc is the number of railway companies (1≤c≤201 \le c \le 20). ss is the index of the starting station and gg is the index of the goal station (1≤s,g≤n1 \le s, g \le n, g≠sg \ne s).

The next mm lines describe the lines. Line ii connects stations xix_i and yiy_i (1≤xi,yi≤n1 \le x_i, y_i \le n, xi≠yix_i \ne y_i) and can be traveled in both directions. Two stations may be connected by more than one line. did_i is the length of line ii (1≤di≤2001 \le d_i \le 200), and cic_i is the index of the company operating it (1≤ci≤c1 \le c_i \le c).

Each company's fare table is a piecewise-linear function of distance. For company jj, pjp_j is the number of segments (1≤pj≤501 \le p_j \le 50). The values qj,kq_{j,k} (1≤k≤pj−11 \le k \le p_j - 1, 1≤qj,k≤100001 \le q_{j,k} \le 10000) are the distances at which the segments change, and rj,kr_{j,k} (1≤k≤pj1 \le k \le p_j, 1≤rj,k≤1001 \le r_{j,k} \le 100) is the fare added per unit distance in segment kk. Writing fj(z)f_j(z) for the fare of distance zz,

fj(z)=fj(z−1)+rj,k(qj,k−1+1≤z≤qj,k),f_j(z) = f_j(z-1) + r_{j,k} \quad (q_{j,k-1}+1 \le z \le q_{j,k}),

where qj,0=0q_{j,0} = 0, fj(0)=0f_j(0) = 0, and qj,pj=∞q_{j,p_j} = \infty.

For example, if pj=3p_j = 3, qj,1=3q_{j,1} = 3, qj,2=6q_{j,2} = 6, rj,1=10r_{j,1} = 10, rj,2=5r_{j,2} = 5, and rj,3=3r_{j,3} = 3, the fare table is:

distance123456789
fare102030354045485154

The values qj,kq_{j,k} increase with kk, and the values rj,kr_{j,k} decrease with kk.

The last dataset is followed by a line containing five zeros separated by spaces, which must not be processed.

Output

For each dataset, output on a single line the minimum possible total fare of a route from the start to the goal. If the goal cannot be reached from the start, output -1 instead. Do not print any extra characters such as trailing spaces.

Given a route, its total fare is computed as follows. Split the route into maximal groups of consecutive lines operated by the same company. For each such group, add the lengths of its lines and use that combined distance to look up the fare from the company's fare table. The total fare of the route is the sum of the fares of these groups. If a line of a different company is used between two lines of the same company, those two lines belong to separate groups and their fares are computed independently. No company offers any transfer discount.

Examples3

  1. Example 1

    Input
    4 4 2 1 4
    1 2 2 1
    2 3 2 1
    3 4 5 1
    2 4 4 2
    3 1
    3 6
    10 5 3
    
    10
    2 0 1 1 2
    1
    
    1
    4 5 2 4 1
    4 3 10 1
    3 2 2 1
    3 2 1 2
    3 2 5 2
    2 1 10 1
    3 3
    20 30
    3 2 1
    5 10
    3 2 1
    5 5 2 1 5
    1 2 10 2
    1 3 20 2
    2 4 20 1
    3 4 10 1
    4 5 20 1
    2 2
    20
    4 1
    20
    3 1
    0 0 0 0 0
    
    Expected output
    54
    -1
    63
    130
    
  2. Example 2

    Input
    3 2 1 1 3
    1 2 5 1
    2 3 5 1
    1
    
    10
    0 0 0 0 0
    
    Expected output
    100
    
  3. Example 3

    Input
    4 2 1 1 4
    1 2 3 1
    2 3 3 1
    1
    
    7
    0 0 0 0 0
    
    Expected output
    -1