洛谷1052(路径压缩后简单dp)

同POJ3744写法都是一样的。

距离太长无意义可以压缩,注意不是随便压的,想一想可以跟%T发生关系。

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 
 7 const int maxn = 2500;
 8 int L, S, T, M, pos[101], a[maxn], f[maxn];
 9 
10 int main() {
11     scanf("%d %d %d %d", &L, &S, &T, &M);
12     for (int i = 1; i <= M; i++)
13         scanf("%d", &pos[i]);
14     sort(pos + 1, pos + 1 + M);
15     for (int i = 1; i <= M; i++) {
16         if (pos[i] - pos[i - 1] > T) {
17             for (int j = M; j >= i; --j)
18                 pos[j] -= pos[i] - pos[i - 1] - T - (pos[i] - pos[i - 1]) % T;
19         }
20     }
21 
22     for (int i = 1; i <= M; i++)    a[pos[i]] = 1;
23     L = pos[M] + 1;
24     for (int i = 1; i <= L; i++)    f[i] = 101;
25     for (int i = 0; i < L; i++) {
26         for (int j = S; j <= T; j++) {
27             int arrive = min(L, i + j);
28             f[arrive] = min(f[arrive], f[i] + a[arrive]);
29         }
30     }
31 
32     printf("%d\n", f[L]);
33     return 0;
34 }

 

转载于:https://www.cnblogs.com/AlphaWA/p/10530773.html

你可能感兴趣的:(洛谷1052(路径压缩后简单dp))