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 ...
随机推荐
- python解析网页中js动态添加的内容
https://www.cnblogs.com/asmblog/archive/2013/05/07/3063809.html https://www.zhihu.com/question/21471 ...
- GridView的经常使用属性
1.android:numColumns="auto_fit" //GridView的列数设置为自己主动 2.android:columnWidth="90dp &q ...
- oracle SQL语句(转)
Oracle数据库语句大全 ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CH ...
- hiberinate二级缓存
hibernate.cfg.xml配置 <!-- 二级缓存类型 --> <property name="hibernate.cache.region.factory_cla ...
- Arcgis Engine(ae)接口详解(2):featureClass查询
//属性查询~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //IQueryFilter代表查询条件,QueryFilterClass代表只限于属性查询(就是没有空间查询) ...
- sanic官方文档解析之Example(一)
1,示例 这部的文档是简单的示例集合,它能够帮助你快速的启动应用大部分的应用,这些应用大多事分类的,并且提供给ini工作的连接代码: 1.1,基础示例 这部分示例集成了提供简单sanic简单的代码 单 ...
- chmod|chown|chgrp和用法和区别
1.chgrp(改变文件所属用户组) chgrp 用户组 文件名 ###就是这个格了.如果整个目录下的都改,则加-R参数用于递归. 如:chgrp -R user smb.conf 2.c ...
- Session 钝化机制
- Hibernate exception
1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...
- Ruby中任务构建工具rake的入门学习教程
参考:http://www.jb51.net/article/81476.htm Rake简介 Rake的意思是Ruby Make,一个用ruby开发的代码构建工具. 但是,为什么Ruby需要Rake ...