HDU 4571 travel in time

Source: Internet
Author: User
Travel in time

Time Limit: 8000/4000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1008 accepted submission (s): 204

Problem description Bob gets tired of playing games, leaves Alice, and travels to Changsha alone. yuelu Mountain, orange Island, window of the world, the Provincial Museum etc... are scenic spots Bob wants to visit. however, his time is very limited,
He can't visit them all.
Assuming that there are n scenic spots in Changsha, Bob defines a satisfaction value Si to each spot. if he visits this spot, his total satisfaction value will plus SI. bob hopes that within the limited time t, he can start at spot S, visit some spots selectively,
And finally stop at spot E, so that the total satisfaction value can be as large as possible. it's obvious that visiting the spot will also cost some time, suppose that it takes CI units of time to visit spot I (0 <= I <n ).
Always remember, Bob can choose to pass by a spot without visiting it (including S and E), maybe he just want to walk shorter distance for saving time.

Bob also has a special need which is that he will only visit the spot whose satisfaction value is
StrictlyLarger than that of which he visited last time. for example, if he has visited a spot whose satisfaction value is 50, he wowould only visit spot whose satisfaction value is 51 or more then. the paths between the spots are bi-directional,
Of course.

Input the first line is an integer W, which is the number of testing cases, and the W sets of data are following.
The first line of each test data contains five integers: n m t s e. N represents the number of spots, 1 <n <100; m represents the number of paths, 0 <m <1000; t represents the time limitation, 0 <t <= 300; s means the spot Bob starts from. e Indicates
The end spot. (0 <= s, e <n)
The second line of the test data contains N integers Ci (0 <= CI <= T), which means the cost of time if Bob visits the spot I.
The third line also has n integers, which means the satisfaction value si that can be obtained by visiting the spot I (0 <= SI <100 ).
The next M lines, each line contains three integers u v l, means there is a bi-directional path between spot U and V and it takes L units of time to walk from u to V or from V to U. (0 <= u, v <n, 0 <= L <= T)

Output output case number in the first line (formatted as the sample output ).
The second line contains an integer, which is the greatest satisfaction value.
If Bob can't reach spot e in T units of time, You shoshould output just a "0" (without quotation marks ).

Sample Input

14 4 22 0 31 1 1 15 7 9 120 1 101 3 100 2 102 3 10

Sample output

Case # There are multiple questions, because the question must ensure that the value is from small to large. Therefore, we can sort the question in order, which leads to a 0-1 backpack problem, it's easy!
# Include <stdio. h> # include <algorithm> # include <string. h >#include <iostream> # define maxn 115 # define maxm 315 # define INF 10000000 using namespace STD; struct node {int ID, Val, cost; bool operator <(node) const {return Val <. val ;}} P [maxn]; int DP [maxn] [maxm], DIS [maxn] [maxn]; int fmin (int A, int B) {if (a <B) return a; return B;} int fmax (int A, int B) {If (A> B) return a; return B ;} bool CMP (node A, Node B) {return A <B ;}int main () {int tcase, TT = 1, t, n, m, S, E, I, J, K; scanf ("% d ", & tcase); While (tcase --) {scanf ("% d", & N, & M, & T, & S, & E); s ++, e ++; memset (DP,-1, sizeof (DP); for (I = 0; I <= N; I ++) {for (j = 0; j <= N; j ++) {dis [I] [J] = inf;} dis [I] [I] = 0 ;} for (I = 1; I <= N; I ++) scanf ("% d", & P [I]. cost), P [I]. id = I; for (I = 1; I <= N; I ++) scanf ("% d", & P [I]. val); for (I = 1; I <= m; I ++) {int SS, EE, temp; scanf ("% d", & SS, & EE, & temp); SS ++, ee ++; Dis [EE] [ss] = dis [ss] [EE] = fmin (DIS [ss] [EE], temp);} For (k = 1; k <= N; k ++) for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) {dis [I] [J] = fmin (DIS [I] [J], DIS [I] [k] + dis [k] [J]);} P [0] = P [s]; sort (p + 1, P + n + 1, CMP); for (I = 0; I <= N; I ++) {J = P [I]. cost + dis [p [I]. id] [s]; If (j <= T) DP [I] [J] = P [I]. val ;}for (I = 1; I <= N; I ++) for (j = 0; j <I; j ++) {int id = P [I]. ID, JD = P [J]. ID; If (P [I]. val> P [J]. val) // It may be equal after sorting, so make sure that the value is greater than for (k = T; k> = dis [ID] [JD] + P [I]. Cost & K> = 0; k --) {If (DP [J] [k-Dis [ID] [JD]-P [I]. Cost]! =-1) DP [I] [k] = fmax (DP [I] [K], DP [J] [k-Dis [ID] [JD]-P [I]. cost] + P [I]. val) ;}} int Maxx = 0; for (I = 0; I <= N; I ++) for (j = 0; j <= T; j ++) {If (J + dis [p [I]. id] [e] <= T) Maxx = fmax (Maxx, DP [I] [J]);} printf ("case # % d: \ n % d \ n ", TT ++, Maxx);} return 0 ;}
Related Article

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.