Fixing the Great Wall
https://odzkskevi.qnssl.com/b7d37f69f479d57e44735fc5d6403983?v=1508326275
【题解】
按照x排序
dp[i][j][0/1]表示从i到j全修好,当前在i/j的最小代价和已知的未修好的时间附加代价(td)
转移即可,即
dp[i][j][0/1] - > d[i-1][j][0]
dp[i][j][0/1] - > d[i][j + 1][1]
为什么这样?想想不这样的情况,会发现都不如这样优秀
还有一个小处理,把起点虚成一个节点做
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <vector> 7 #define min(a, b) ((a) < (b) ? (a) : (b)) 8 #define max(a, b) ((a) > (b) ? (a) : (b)) 9 10 inline void swap(int &a, int &b) 11 { 12 long long tmp = a;a = b;b = tmp; 13 } 14 15 inline void read(int &x) 16 { 17 x = 0;char ch = getchar(), c = ch; 18 while(ch < '0' || ch > '9')c = ch, ch = getchar(); 19 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 20 } 21 22 const int INF = 0x3f3f3f3f; 23 const int MAXN = 10000 + 10; 24 25 int n, v, s, sum[MAXN]; 26 struct Node 27 { 28 int x,c,d; 29 }node[MAXN]; 30 31 int cmp(Node a, Node b) 32 { 33 return a.x < b.x; 34 } 35 36 //dp[i][j][0/1]表示修复i~j这段区间,当前在最左端(0), 最右端(1)的 37 //最小花费 38 39 double dp[MAXN][MAXN][2]; 40 41 int main() 42 { 43 while(scanf("%d %d %d", &n, &v, &s) != EOF && n && v && s) 44 { 45 for(register int i = 1;i <= n;++ i) 46 read(node[i].x), read(node[i].c), read(node[i].d); 47 node[n + 1].x = s; 48 node[n + 1].c = 0; 49 node[n + 1].d = 0; 50 ++ n; 51 std::sort(node + 1, node + 1 + n, cmp); 52 for(register int i = 1;i <= n;++ i) 53 sum[i] = sum[i - 1] + node[i].d; 54 for(register int i = 1;i <= n;++ i) 55 for(register int j = 1;j <= n;++ j) 56 { 57 if(i == j && node[i].x == s && node[i].c == 0 && node[i].d == 0) dp[i][j][0] = dp[i][j][1] = 0; 58 else dp[i][j][0] = dp[i][j][1] = INF; 59 } 60 for(register int len = 1;len <= n;++ len) 61 { 62 for(register int i = 1;i <= n;++ i) 63 { 64 int j = i + len - 1; 65 dp[i - 1][j][0] = min(dp[i - 1][j][0], 66 min(dp[i][j][0] + (double)(node[i].x - node[i - 1].x)/v * (sum[i - 1] + sum[n] - sum[j]) + node[i - 1].c, 67 dp[i][j][1] + (double)(node[j].x - node[i - 1].x)/v * (sum[i - 1] + sum[n] - sum[j]) + node[i - 1].c)); 68 dp[i][j + 1][1] = min(dp[i][j + 1][1], 69 min(dp[i][j][0] + (double)(node[j + 1].x - node[i].x)/v * (sum[i - 1] + sum[n] - sum[j]) + node[j + 1].c, 70 dp[i][j][1] + (double)(node[j + 1].x - node[j].x)/v * (sum[i - 1] + sum[n] - sum[j]) + node[j + 1].c)); 71 } 72 } 73 printf("%d ", min((int)dp[1][n][0], (int)dp[1][n][1])); 74 } 75 return 0; 76 }