POJ2355――Railway tickets

Railway tickets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2710   Accepted: 962


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







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.


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⑴ 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.


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 40
2 6

Sample Output



Ural Collegiate Programming Contest 1999


#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;
const int N = 10010;
int dp[N];
int sta[N];

int main()
int l1, l2, l3;
int c1, c2, c3;
int n, s, e;
while(~scanf("%d%d%d%d%d%d", &l1, &l2, &l3, &c1, &c2, &c3))
// printf("%I64d
", inf);
scanf("%d", &n);
scanf("%d%d", &s, &e);
if (s > e)
swap(s, e);
memset (dp, inf, sizeof(dp));
sta[1] = 0;
dp[e] = 0;
for (int i = 2; i <= n ; ++i)
scanf("%d", &sta[i]);
for (int i = e – 1; i >= s; –i)
for (int j = i; j <= e; ++j)
if (sta[j] – sta[i] > l3)
if (sta[j] – sta[i] <= l1)
dp[i] = min(dp[i], dp[j] + c1);
else if (sta[j] – sta[i] <= l2)
dp[i] = min(dp[i], dp[j] + c2);
dp[i] = min(dp[i], dp[j] + c3);
", dp[s]);
return 0;

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » POJ2355――Railway tickets


Hi, 如果你对这款模板有疑问,可以跟我联系哦!

赞助VIP 享更多特权,建议使用 QQ 登录