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 C i 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 C i ( 0 <= C i <= 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 <= S i < 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

 

转载于:https://www.cnblogs.com/contestant/archive/2013/06/05/3118180.html

降低这段代码的重复率:def calTravelCost(route_list,model): timetable_list=[] distance_of_routes=0 time_of_routes=0 obj=0 for route in route_list: timetable=[] vehicle=model.vehicle_dict[route[0]] travel_distance=0 travel_time=0 v_type = route[0] free_speed=vehicle.free_speed fixed_cost=vehicle.fixed_cost variable_cost=vehicle.variable_cost for i in range(len(route)): if i == 0: next_node_id=route[i+1] travel_time_between_nodes=model.distance_matrix[v_type,next_node_id]/free_speed departure=max(0,model.demand_dict[next_node_id].start_time-travel_time_between_nodes) timetable.append((int(departure),int(departure))) elif 1<= i <= len(route)-2: last_node_id=route[i-1] current_node_id=route[i] current_node = model.demand_dict[current_node_id] travel_time_between_nodes=model.distance_matrix[last_node_id,current_node_id]/free_speed arrival=max(timetable[-1][1]+travel_time_between_nodes,current_node.start_time) departure=arrival+current_node.service_time timetable.append((int(arrival),int(departure))) travel_distance += model.distance_matrix[last_node_id, current_node_id] travel_time += model.distance_matrix[last_node_id, current_node_id]/free_speed+\ + max(current_node.start_time - arrival, 0) else: last_node_id = route[i - 1] travel_time_between_nodes = model.distance_matrix[last_node_id,v_type]/free_speed departure = timetable[-1][1]+travel_time_between_nodes timetable.append((int(departure),int(departure))) travel_distance += model.distance_matrix[last_node_id,v_type] travel_time += model.distance_matrix[last_node_id,v_type]/free_speed distance_of_routes+=travel_distance time_of_routes+=travel_time if model.opt_type==0: obj+=fixed_cost+travel_distance*variable_cost else: obj += fixed_cost + travel_time *variable_cost timetable_list.append(timetable) return timetable_list,time_of_routes,distance_of_routes,obj
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值