POJ 2355 Railway tickets(线段dp 坐车买票问题)

来源:互联网 发布:win10吃鸡优化 编辑:程序博客网 时间:2024/04/28 08:42
Railway tickets
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 3089
Accepted: 1097

Description

The railway line "Ekaterinburg-Sverdlovsk" with several stations has been built. This railway line can be represented as a line segment, railway stations being points on it. The railway line starts at the station "Ekaterinburg" and finishes at the station "Sverdlovsk", so stations are numbered starting from "Ekaterinburg" (it has number 1) and "Sverdlovsk" is the last station. 


Cost of the ticket between any two stations depends only on a distance between them. The prices for the tickets are 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 be booked if and only if the distance between these station does not exceed L3. So sometimes it is necessary to book several tickets to pay for the parts of the whole way between stations.

For example, on the railway line shown at the figure above there are seven stations. The direct ticket from the second station to the sixth one can not be booked. There are several ways to pay for the travel between these stations. One of them is to book two 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 is equal to 2*L2, the whole travel can not be paid by booking two tickets at price C2, because each ticket is valid for only one travel and each travel should start and end only at stations. 

Your task is to write a program, that will find the minimal cost of the travel between two given stations.

Input

The first line of the input file 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 two different integers separated by space. They represent serial numbers of stations, the travel between which must be paid. Next N-1 lines contain distances from the first station ("Ekaterinburg") on the railway line to others. These distances are given as different positive integers and are arranged in the ascending order. The distance from "Ekaterinburg" to "Sverdlovsk" does not exceed 10^9. The distance between any neighboring stations does not exceed L3. The minimal travel cost between two given stations will not exceed 10^9.

Output

Program should print to the output file the only number, which is the minimal travel cost between two given stations.

Sample Input

3 6 8 20 30 4072 6378131523

Sample Output

70

Source

Ural Collegiate Programming Contest 1999
题目大意:
X为距离 , 当0<X<=L1, 票价为C1。 L1<X<=L2 ,票价为C2。L2<X<=L3,票价为C3。给每段车站时间的距离,求某两个车站之间的总票价的最小值。
动态转移方程:
1、dp[ i ]表示从a到 i 的最小票价
2、dp[ i ]=min(do[ i ] , dp[ j ]+ j 到 i 的票价
AC代码
#include<cstdio>#include<iostream>using namespace std;int f[10005];int dp[10004];int main(){int l1,l2,l3,c1,c2,c3;scanf("%d%d%d%d%d%d",&l1,&l2,&l3,&c1,&c2,&c3);int n;scanf("%d",&n);int a,b;scanf("%d%d",&a,&b);if(a>b)swap(a,b);for(int i=2;i<=n;i++){scanf("%d",&f[i]);}for(int i=a+1;i<=b;i++){dp[i]=1000000005;for(int j=i-1;j>=a;j--){int count=f[i]-f[j];if(count>l3)break;int m;if(count>0&&count<=l1)m=c1;else if(count>l1&&count<=l2)m=c2;else m=c3;dp[i]=min(dp[i],dp[j]+m);}}printf("%d\n",dp[b]);return 0;}


原创粉丝点击