Ural1031--dp--railway Tickets

Source: Internet
Author: User

Description

The railway line "Yekaterinburg-sverdlovsk" with several stations have been built. This railway line can is represented as a line segment, railway stations being points on it. The railway line starts at the station ' Yekaterinburg ' and finishes at the station ' Sverdlovsk ', so stations is numbered Starting from "Yekaterinburg" (it had number 1) and "Sverdlovsk" is the last station. Cost of the ticket between any and stations depends only on a distance between them. The prices for the tickets is specified in the following table.
Distance X between stations Price for the ticket
0 < XL1 C1
L1 < Xl2 C2
L2 < Xl3 C3
Direct tickets from one station to another can is booked if and only if the distance between these station does not exceed L3. So sometimes it was necessary to book several tickets to pay for the parts of the whole by between. For example, on the railway line shown at the figure above there is seven stations. The direct ticket from the second station to the sixth one can is booked. There is several ways to pay for the travel between these stations. One of them is to book the Tickets:one ticket at price C2 to travel between the second and the third stations, and other at price C3 to travel between the third and the sixth stations. Note, that though the distance between the second and the sixth stations are equal to 2 L2, the whole travel can is paid by booking-tickets at price C2, because each ticket are valid for the only one travel and all travel should start and end only at stations. Your task is-to-write a program, which would find the minimal cost of the travel between the given stations.

Input

The first line of the input contains 6 integers L1, L2, L3, C1, C2, C3 (1≤ L1 < L2 < L3≤10 9, 1≤ C1 < C2 < C3≤10 9) in the specified order with one space between. The second line contains the amount of stations N(2≤ N≤10000). The third line contains and different integers separated by space. They represent serial numbers of stations, the travel between which must is paid. Next N−1 lines contain distances from the first station ("Yekaterinburg") on the railway line to others. These distances is given as different positive integers and is arranged in the ascending order. The distance from ' Yekaterinburg ' to ' Sverdlovsk ' does not exceed 10 9. The distance between any neighboring stations does not exceed L3. The minimal travel cost between the given stations would not exceed 10 9.

Output

program should print to the output of the only number, which are the minimal travel cost between the given stations.

Sample Input

input Output
3 6 8 20 30 4072 6378131523
70
The main idea: the first input distance interval and the price range n means that there are n stations, we want to arrive from S1 to S2, next n-1 line represents the distance from the first station of each station except the first station, ask you to the minimum cost from S1 to S2 definition Dp[i] Represents the money that is currently being used at the station I stations the state transfer equation Dp[i] = min (dp[i],dp[j]+c[j), so write an expression to find out the range of J, or too weak orz, can not write the original
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int dp[10010];int l[4],c    [4],s[10010];const int inf = 0x3f3f3f3f;int Main () {int s1,s2;    int n;        while (~SCANF ("%d%d%d%d%d%d", &l[1],&l[2],&l[3],&c[1],&c[2],&c[3]) {scanf ("%d", &n);        scanf ("%d%d", &AMP;S1,&AMP;S2);            if (S1 > s2) {int temp = S1;            S1 = s2;        S2 = temp;        } S[1] = 0;        for (int i = 2; I <= n; i++) scanf ("%d", &s[i]);        for (int i = 1; I <= n;i++) dp[i] = inf;        DP[S1] = 0;                for (int k = S1 + 1, k <= S2; k++) {for (int p = 1; p <= 3; p++) {int j = k-1;                    while (J >= S1) {if (S[k] – S[J] > L[p]) break;                    Dp[k] = min (dp[k],dp[j]+c[p]);                j--;    }}} printf ("%d\n", Dp[s2]); } RETUrn 0;} 

  

Ural1031--dp--railway Tickets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.