码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 1025 A Spy in the Metro(DAG dp)

时间:2016-03-14 13:54:59      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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)

标签:

原文地址:http://www.cnblogs.com/zstu-jack/p/5275292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!