Travel in time

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 strictly larger than that of which he visited last time. For example, if he has visited a spot whose satisfaction value is 50, he would 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 should output just a “0” (without quotation marks).
Sample Input
1
4 4 22 0 3
1 1 1 1
5 7 9 12
0 1 10
1 3 10
0 2 10
2 3 10
Sample Output
Case #1: 21
Source
 1 /*看到那个要求递增的限制很容易想到DP,把所有节点(N<100)按Si递增排序,
 2 只有在访问了前面的节点才能访问后面的节点。对于每个节点,保存剩余的时间。
 3 f[i][j] = max(f[k][ j+dist[i][k]+c[i] ] + s[i])
 4 i - 处理到第i个节点; j - 剩余j单位的时间;dist[i][k] - 从节点i到节点k所需要的时间*/
 5 
 6 #include <stdio.h>
 7 #include <algorithm>
 8 #include <cstring>
 9 using namespace std;
10 
11 struct node {
12     int c,s,id;
13     bool operator <(node temp) const {
14         return s<temp.s;
15     }
16 } a[200];
17 int dist[110][110];
18 int S,N;
19 
20 void Floyd() {
21     for (int i = 1; i <= N; i++) dist[i][i] = 0;
22     for (int k = 1; k <= N; k++)
23         for (int i = 1; i <= N; i++)
24             for (int j = 1; j <= N; j++)
25                 if (dist[i][j] > dist[i][k]+dist[k][j])
26                     dist[i][j] = dist[i][k] + dist[k][j];
27     for (int i = 1; i <= N; i++)
28         dist[0][i] = dist[S][i];
29 }
30 
31 int main() {
32    // freopen("test.txt","r",stdin);
33     int W,cas=0;
34     scanf("%d",&W);
35     while (cas++<W) {
36         int M,T,E;
37         scanf("%d%d%d%d%d",&N,&M,&T,&S,&E);
38         ++S,++E;
39         for (int i = 1; i <= N; i++) scanf("%d",&a[i].c);
40         for (int i = 1; i <= N; i++) {
41             scanf("%d",&a[i].s);
42             a[i].id = i;
43         }
44         memset(dist,0x3f,sizeof(dist));
45         for (int i = 1; i <= M; i++) {
46             int u,v,l;
47             scanf("%d%d%d",&u,&v,&l);
48             ++u,++v;
49             if (l < dist[u][v])
50                 dist[u][v] = dist[v][u]= l;
51         }
52         a[0].c = a[0].id = 0;
53         a[0].s = -1;
54         sort(a+1,a+N+1);
55         Floyd();
56         if (dist[S][E] > T) {
57             printf("Case #%d:\n0\n",cas);
58             continue;
59         }
60 
61         int ans = 0;
62         int f[110][310];
63         memset(f,-0x3f,sizeof(f));
64         f[0][T] = 0;
65         for (int i = 1; i <= N; i++) {
66             int u = a[i].id;
67             for (int j = 0; j <= T; j++) {
68                 for (int k = 0; k < i; k++)
69                 if (a[k].s < a[i].s) {
70                         int v = a[k].id;
71                         int tmp = j+dist[v][u]+a[i].c;
72                         if (tmp <= T) f[i][j] = max(f[i][j],f[k][tmp]+a[i].s);
73                     }
74                 if (j >= dist[u][E]) ans = max(ans,f[i][j]);
75             }
76         }
77         printf("Case #%d:\n%d\n",cas,ans);
78     }
79 }
View Code

 

posted @ 2013-06-05 00:38  1002liu  阅读(261)  评论(0编辑  收藏  举报