UVA 1025 A Spy in the Metro(DAG dp)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 1025 A Spy in the Metro(DAG dp)相关的知识,希望对你有一定的参考价值。

UVA 1025

 

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<queue>
 4 #include<vector>
 5 #include<stack>
 6 #include<set>
 7 #include<string>
 8 #include<cstring>
 9 #include<math.h>
10 #include<algorithm>
11 #include<map>
12 
13 #include<sstream>
14 #include<ctype.h>
15 
16 
17 #define lson l,m,rt<<1
18 #define rson m+1,r,rt<<1|1
19  
20 typedef long long ll;
21 using namespace std;
22  
23 const int INF = 0x7f7f7f7f;
24 const double PI = acos(-1.0);
25 const int maxn = 100005;
26 #define LOCAL 0
27 #define MOD 1000000007
28 
29 
30 int n,T;
31 int l,r;
32 int ls[55],rs[55];
33 int timecost[55];
34 int dp[210][55];
35 bool has_train[210][55][2];
36 int kase = 1;
37 
38 void input_pre(){
39     memset(dp,0,sizeof(dp));
40     memset(timecost,0,sizeof(timecost));
41     memset(has_train,0,sizeof(has_train));
42     scanf("%d",&T);
43     for(int i = 1 ; i < n ; i ++){
44         scanf("%d",&timecost[i]);
45     }
46     scanf("%d",&l);    // l 辆车 
47     for(int i = 0 ; i < l ; i ++){
48         scanf("%d",&ls[i]);    //发车时间 
49         has_train[ls[i]][1][0] = true;
50     }
51     for(int i = 0 ; i < l ; i ++){
52         int time = ls[i];
53         for(int j = 2 ; j <= n ; j ++){
54             time += timecost[j-1];
55             has_train[time][j][0] = true;
56         }        
57     }     
58     scanf("%d",&r);
59     for(int i = 0 ; i < r ; i ++){
60         scanf("%d",&rs[i]);
61         has_train[rs[i]][n][1] = true;
62     }
63     for(int i = 0 ; i < r ; i ++){
64         int time = rs[i];
65         for(int j = n-1 ; j >= 1 ; j --){
66             time += timecost[j];
67             has_train[time][j][1] = true;
68         }
69     }
70 
71     for(int i = 1 ; i <= n - 1 ; i ++)    dp[T][i] = INF;
72     dp[T][n] = 0;    
73 }
74 void solve(){
75     for(int i = T-1 ; i >= 0 ; i --){
76         for(int j = 1 ; j <= n ; j ++){
77             dp[i][j] = dp[i+1][j] + 1;
78             if(j < n && has_train[i][j][0] && i+timecost[j] <= T)
79                 dp[i][j] = min(dp[i][j],dp[i+timecost[j]][j+1]);
80             if(j > 1 && has_train[i][j][1] && i+timecost[j-1] <= T)
81                 dp[i][j] = min(dp[i][j],dp[i+timecost[j-1]][j-1]);
82                 
83         }
84     }
85     printf("Case Number %d: ",kase++);
86     if(dp[0][1] >= INF) cout << "impossible" << endl;
87     else printf("%d\n",dp[0][1]);
88     
89 }
90 
91 int main(){
92     while(scanf("%d",&n) != EOF && n){
93         input_pre();
94         solve();
95     }
96     return 0;
97 }

 

以上是关于UVA 1025 A Spy in the Metro(DAG dp)的主要内容,如果未能解决你的问题,请参考以下文章

UVa 1025 A Spy in the Metro(动态规划)

UVA 1025 A Spy in the Metro(DAG dp)

UVA 1025 A Spy in the Metro

UVa 1025 A Spy in the Metro

UVA 1025 -- A Spy in the Metro (DP)

UVA_1025_A_Spy_in_the_Metro_(动态规划)