Hundred practice/2017 postgraduate push-off Test E:railway tickets

Source: Internet
Author: User
Tags ticket

Title Source: http://poj.org/problem?id=2355 Railway Tickets

-----------------------------------------------------

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 3418

Accepted: 1188

Description

The railway line "Ekaterinburg-sverdlovsk" with several stations have been built. Thisrailway Line can is represented as a line segment, railway stations beingpoints on it. The railway line starts at the station ' Ekaterinburg ' and finishes at the station ' Sverdlovsk ', so stations is Numberedsta Rting from "Ekaterinburg" (it had number 1) and "Sverdlovsk" is the last station.


Cost of the ticket between any and stations depends only on a distance betweenthem. The prices for the tickets is specified in the following table.

Distance between stations-x

Price for the ticket

0<x<=l1

C1

L1<x<=l2

C2

L2<x<=l3

C3


Direct tickets from one station to another can is booked if and only if thedistance between these station does not exceed L3. So sometimes it was Necessaryto book several tickets to pay for the parts of the whole-a-do betweenstations.

For example, on the railway line shown at the figure above there is sevenstations. The direct ticket from the second station to the sixth one can not bebooked. There is several ways to pay for the travel between these stations. One of them is to book Tickets:one ticket at Price C2 to travel betweenthe second and the third stations, and other A T price C3 to travel between Thethird and the sixth stations. Note, that though the distance between the Secondand the sixth stations are equal to 2*L2 and the whole travel can isn't be paid Bybooking tickets at the price C2, because each ticket are valid for only onetravel and each travel should start and end O Nly at stations.

Your task is-to-write a program, which would find the minimal cost of the travelbetween and the given stations.

Input

The first line of the input file contains 6integers 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). Thethird line contains, different integers separated by space. They representserial numbers of stations, the travel between which must is paid. Next n-1lines contain distances from the first station ("Ekaterinburg") onthe railway line to others. These distances is given as different positiveintegers and is arranged in the ascending order. The distance from ' Ekaterinburg ' to ' Sverdlovsk ' does not exceed 10^9. Thedistance between any neighboring stations does not exceed L3. The minimaltravel cost between the given stations would not exceed 10^9.

Output

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

Sample Input

3 6 8 20 30 40

7

2 6

3

7

8

13

15

23

Sample Output

70

-----------------------------------------------------

Thinking of solving problems

One-dimensional dynamic programming problem (can also be considered as a two-dimensional dynamic programming solution, but the time complexity of the super)
Difficulty: There are 3 types of tickets, with 3 double-ended queue to maintain a distance of the current compute node is less than equal to the ticket path of the site
Such time complexity O (n). The essence of this is to circumvent the search process of the nodes that are searching forward in the ticket range.

-----------------------------------------------------

Code

Algorithm: Dynamic Programming/* This version draws on the ideas in discuss and can be AC/#include <limits.h> #include <vector> #include <algorithm> #inc

Lude<iostream> #include <queue> using namespace std;
    int main () {vector<int> L;
    Vector<int> C;
    int i=0, tmp=0, j=0, k=0;
        for (i=0; i<3; i++) {cin >> tmp;
    L.push_back (TMP);
        } for (i=0; i<3; i++) {cin >> tmp;
    C.push_back (TMP);
    } int sN = 0;                          Cin >> SN;
    # of stations int src=0, dst=0;
    CIN >> src >> DST;
    Vector<int> Len;                   Len.push_back (0);
        Compatible with the departure for (i=0; i<sn-1; i++) {cin >> tmp;
    Len.push_back (TMP);
		} if (dst = = src)//departure = terminal {cout << 0;
	return 0;
	} else if (DST < SRC)//The originating station is behind the terminal {swap (DST,SRC);
    } int m = DST-SRC;                                       Vector<int> dp0; Dynamic Programming Array 0 deque<int> Q[3];
	Secondary bidirectional queue: Site (k=0,1,2)/initialization for (i=0; i<3; i++) {q[i].push_back (src-1) <=l[k] (j+src-1);
	} dp0.push_back (0);
	for (I=1; i<m+1; i++) {dp0.push_back (Int_max); }//DP loop for (j=1; j<m+1; J + +) {for (k=0, k<3; k++) {while (!q[k].empty () && len.at (q[k].fr
			Ont ()) +l.at (k) <len.at (src+j-1)) {Q[k].pop_front ();
			} if (!q[k].empty ()) {dp0.at (j) = Min (dp0.at (j), Dp0.at (Q[k].front ()-src+1) +c.at (k)); }} for (k=0, k<3; k++) {while (!q[k].empty () && dp0.at (Q[k].back ()-src+1) >=dp0.at (j)) {q[
			K].pop_back ();
		} q[k].push_back (j+src-1);
	
	}} cout << dp0.at (m);
return 0; }


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.