UVa 1632 阿里巴巴(区间DP)
生活随笔
收集整理的這篇文章主要介紹了
UVa 1632 阿里巴巴(区间DP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://vjudge.net/problem/UVA-1632
題意:
直線上有n個點,其中第i個點的坐標是xi,且它會在di秒之后消失。Alibaba可以從任意位置出發,求訪問完所有點的最短時間。
?
思路:
區間DP。
d[i][j][0]用來表示訪問完區間 i ~ j 之間所有點的最短時間,并且此時處于 i 點,相反的,d[i][j][1]表示的是處于 j 點。
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn = 10000 + 5; 8 const int INF = 10000000; 9 10 int a[maxn], b[maxn]; 11 int n; 12 int d[maxn][maxn][2]; 13 14 int main() 15 {17 while (cin >> n) 18 { 19 memset(d, 0, sizeof(d)); 20 for (int i = 1; i <= n; i++) 21 cin >> a[i] >> b[i]; 22 for (int i = n; i >=1; i--) 23 { 24 for (int j = i + 1; j <= n; j++) 25 { 26 d[i][j][0] = min(d[i + 1][j][0] + a[i + 1] - a[i], d[i + 1][j][1] + a[j] - a[i]); 27 if (d[i][j][0] >= b[i]) d[i][j][0] = INF; 28 d[i][j][1] = min(d[i][j-1][1] + a[j] - a[j - 1], d[i][j - 1][0] + a[j] - a[i]); 29 if (d[i][j][1] >= b[j]) d[i][j][1] = INF; 30 } 31 } 32 int ans = min(d[1][n][0], d[1][n][1]); 33 if (ans == INF) puts("No solution"); 34 else printf("%d\n", ans); 35 } 36 return 0; 37 }?
轉載于:https://www.cnblogs.com/zyb993963526/p/6381886.html
總結
以上是生活随笔為你收集整理的UVa 1632 阿里巴巴(区间DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: For循环中不可以嵌套RDD操作
- 下一篇: 设计时数据源:在PostgreSql 数