UVa 1336 Fixing the Great Wall (区间DP)
题意:给定 n 个结点,表示要修复的点,然后机器人每秒以 v 的速度移动,初始位置在 x,然后修复结点时不花费时间,但是如果有的结点暂时没修复,
那么每秒它的费用都会增加 d,修复要花费 c,坐标是 pos,问你最少花费是多少。
析:dp[i][j][k] 表示已经修复了 i-j 区间,并且当前在 k,那么两种方案,向左移动,或者向右移动,最后输出就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define print(a) printf("%d\n", (a))
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int pos, cost, det;
bool operator < (const Node &p) const{
return pos < p.pos;
}
};
Node a[maxn];
double sum[maxn], v;
double dp[maxn][maxn][2]; double cal(int i, int j, int l, int r){
double t = 1.0 * fabs(a[l].pos-a[r].pos) / v;
double ans = sum[i-1] + sum[n+1] - sum[j];
return ans * t;
} int solve(){
for(int i = 0; i <= n+1; ++i)
for(int j = 0; j <= n+1; ++j)
dp[i][j][0] = dp[i][j][1] = inf; int p = lower_bound(a+1, a+n+1, (Node){m, 0, 0}) - a;
dp[p][p][0] = dp[p][p][1] = 0.0; for(int i = p; i > 0; --i){
for(int j = p; j <= n+1; ++j){
dp[i-1][j][0] = min(dp[i-1][j][0], dp[i][j][0]+cal(i, j, i, i-1)+a[i-1].cost);
dp[i-1][j][0] = min(dp[i-1][j][0], dp[i][j][1]+cal(i, j, j, i-1)+a[i-1].cost);
dp[i][j+1][1] = min(dp[i][j+1][1], dp[i][j][0]+cal(i, j, i, j+1)+a[j+1].cost);
dp[i][j+1][1] = min(dp[i][j+1][1], dp[i][j][1]+cal(i, j, j, j+1)+a[j+1].cost);
}
}
return min(dp[1][n+1][0], dp[1][n+1][1]);
} int main(){
while(scanf("%d %lf %d", &n, &v, &m) == 3 && n+v+m){
for(int i = 1; i <= n; ++i) scanf("%d %d %d", &a[i].pos, &a[i].cost, &a[i].det);
a[n+1].pos = m; a[n+1].cost = a[n+1].det = 0;
sort(a+1, a+n+2);
sum[0] = 0;
for(int i = 1; i <= n+1; ++i) sum[i] = sum[i-1] + a[i].det; int ans = solve();
printf("%d\n", ans);
}
return 0;
}
UVa 1336 Fixing the Great Wall (区间DP)的更多相关文章
- 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall
UVa 10618 Fixing the Great Wall 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...
- UVA 10529 - Dumb Bones(概率+区间dp)
UVA 10529 - Dumb Bones option=com_onlinejudge&Itemid=8&category=518&page=show_problem&am ...
- uva 10304 - Optimal Binary Search Tree 区间dp
题目链接 给n个数, 这n个数的值是从小到大的, 给出个n个数的出现次数. 然后用他们组成一个bst.访问每一个数的代价是这个点的深度*这个点访问的次数. 问你代价最小值是多少. 区间dp的时候, 如 ...
- UVA 10891 Game of Sum(区间DP(记忆化搜索))
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 10453 【回文串区间dp】
Uva 10453 题意:给定字符串,问最少插入多少个字符使其变成回文串,并任意输出一种结果. 题解:和Uva 10739类似,这里是只能增加.类似定义dp[i][j]表示子串Si...Sj变为回文串 ...
- UVA - 10891 Game of Sum (区间dp)
题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...
- UVA 1626 区间dp、打印路径
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...
- UVA-1336 Fixing the Great Wall(区间DP)
题目大意:长城(视作x正半轴)有n处破损.有一个智能修复机器人,它的初始位置和移动速度已知.每处破损处都有一组参数(x,c,d),x表示位置,c.d表示在时间t后再修复该处破损的花费为d*t+c.求用 ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
随机推荐
- es删除文档或者删除索引
es删除文档或者删除索引 学习了:https://www.imooc.com/video/15771 删除文档: DELETE http://127.0.0.1:9200/people/man/1 删 ...
- google 集群计算的3大基础设施
1. GFS 分布式文件系统 2. map-reduce 分布式计算框架 3. bigtable 海量key-value的存储 (开源实现:Hypertable)
- [poj 2331] Water pipe ID A*迭代加深搜索(dfs)
Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...
- poj 3169 Layout(差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6549 Accepted: 3168 Descriptio ...
- hdoj-3371-Connect the Cities【最小生成树】
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- c程序设计语言第一章1
1,c程序都是由函数和变量组成的. 练习1.6验证布尔表达式getchar()!= EOF的取值是0还是1 答: #include <stdio.h> #include <stdli ...
- HDU 2578 Dating with girls(1) [补7-26]
Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 凝视转换(c转换为c++)
C语言凝视->C++凝视即/*xxxxx*/->//xxxxx 在转换凝视前我们先了解一个概念:什么是有限状态机? 有限状态机FSM是软件上经常使用的一种处理方法,它把复杂的控制逻辑分解成 ...
- MaterialImageLoading
https://github.com/eltld/MaterialImageLoading
- unity视频播放,
PC端视频播放: Unity中实现PC端播放视频,非常easy用到MovieTexture属于贴图Texture的子类. 在播放视频之前.我们得记得下载quicktime插件,仅仅有导入了quickt ...