【URAL1039】RAILWAY TICKETS火车票
2010-10-04 09:59
330 查看
The railway line “Yekaterinburg-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 “Yekaterinburg” and finishes at the station “Sverdlovsk”, so stations are numbered starting from “Yekaterinburg” (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 X between stations | price for the ticket |
0 < X ≤ L1 | C1 |
L1 < X ≤ L2 | C2 |
L2 < X ≤ L3 | C3 |
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 2L2, 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 contains 6 integers L1, L2, L3, C1, C2, C3 (1 ≤ L1 < L2< L3 ≤ 109, 1 ≤ C1 < C2 < C3 ≤ 109) 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 (“Yekaterinburg”) on the railway line to others. These distances are given as different positive integers and are arranged in the ascending order. The distance from “Yekaterinburg” to “Sverdlovsk” does not exceed 109. The distance between any neighboring stations does not exceed L3. The minimal travel cost between two given stations will not exceed 109.Output
Program should print to the output the only number, which is the minimal travel cost between two given stations.#include<stdio.h> #include<stdlib.h> int l1,l2,l3,c1,c2,c3; int st,en,n; int dis[10000],f[10000]; int k1,k2,k3; int i,j; int min(int a,int b) { if (a<b) return a; else return b; } int main() { //freopen("input.txt","r",stdin); scanf("%d %d %d %d %d %d",&l1,&l2,&l3,&c1,&c2,&c3); scanf("%d",&n); scanf("%d %d",&st,&en); if (st>en) { int temp=st; st=en; en=temp; } for (i=2;i<=n;i++) scanf("%d",&dis[i]); k1=k2=k3=st; f[st]=0; for (i=st+1;i<=en;i++) { /* for (j=k1+1;j<=en;j++) if (dis[j]-dis[k1]<=l1) {f[j]=min(f[j],f[k1]+c1);} else {k1=j-1;break;} for (j=k2+1;j<=en;j++) if (dis[j]-dis[k2]<=l2 && dis[j]-dis[k2]>l1) {f[j]=min(f[j],f[k2]+c2);} else {if (dis[j]-dis[k2]>l2) {k2=j-1;break;}} for (j=k3+1;j<=en;j++) if (dis[j]-dis[k3]<=l3 && dis[j]-dis[k3]>l2) {f[j]=min(f[j],f[k3]+c3);} else {if (dis[j]-dis[k3]>l3) {k3=j-1;break;}} */ f[i]=1000000000; while (dis[i]-dis[k1]>l1) k1++; while (dis[i]-dis[k2]>l2) k2++; while (dis[i]-dis[k3]>l3) k3++; f[i]=min(f[i],f[k1]+c1); f[i]=min(f[i],f[k2]+c2); f[i]=min(f[i],f[k3]+c3); } printf("%d/n",f[en]); return 0; }
相关文章推荐
- 【URAL1039】RAILWAY TICKETS火车票
- ural 1039 Anniversary Party
- ural 1039 没有上司的晚会 树形dp
- URAL 1031. Railway Tickets(spfa)
- 树形DP URAL 1039 Anniversary Party
- URAL 1039 / poj2342-Anniversary Party-树形DP
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
- URAL 1031 Railway Tickets
- URAL 1039 Anniversary Party 树形dp
- ural 1039 Anniversary Party
- ural 1039
- URAL 1039 Anniversary Party 树形dp
- [Ural 1039] 没有上司的晚会
- 【URAL1031】Railway tickets
- ural 1031 Railway Tickets
- URAL1031——DP——Railway Tickets
- ural 1039 anniversary party
- ural 1039 Anniversary Party
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party
- URAL 1039 Anniversary Party