UVa 1628 Pizza Delivery

题目:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189

思路:

  本体与修缮长城一题有所相似。所以解法有相似之处。

不同之处就是本体可能会产生负情况,即送餐时间晚了客户会反过来找你要钱所以需要放弃,但修缮长城只有费用,顺手修了肯定是一个不错的选择。

依旧将区间两端与位置作为状态不过要添加一维cnt表示还需要送餐的人数。类似地定义:d[i][j][cnt][p]表示已经送完了ij区间(区间内或送餐或放弃)位于p(p==0||p==1)还剩下cnt个客户需要继续送餐。添加的一维成功解决了不知道还有多少的客户需要送餐的问题。这里注意到DP过程中利用的信息都来源于状态,因此定义的状态必须要提供转移足够的信息,这样才能得到所需要的值。

转移方程:

d[i][j][cnt][p]=max{d[i][k][cnt-1][1]+pay[knew]//k在余下的右区间  ,  d[k][j][cnt-1][0] +pay[knew]//k在余下的左区间}

代码:

 // UVa1628 Pizza Delivery
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ; int kase, n;
int p[maxn], v[maxn];
int d[maxn][maxn][maxn][];
int vis[maxn][maxn][maxn][]; // already considered s~e, still need to delivery to cnt people.
// pos = 0 means at s, pos = 1 means at e
int dp(int s, int e, int cnt, int pos) {
if(cnt == ) return ; //cnt==0 return int &ans = d[s][e][cnt][pos]; //记忆化搜索
if(vis[s][e][cnt][pos] == kase) return ans;
vis[s][e][cnt][pos] = kase; ans = ;
//枚举的i与之前区间相间的部分默认为不会送餐 //比较得出max_ans
if(!pos) { //pos==s
for(int i = ; i < s; i++) //s->i //i在区间的左边
ans = max(ans, v[i] - cnt * abs(p[i] - p[s]) + dp(i, e, cnt - , ));
//ans=max(ans,足够已知的未来价值+子问题价值)
for(int i = e + ; i < n; i++) //s->i //i在区间的右边
ans = max(ans, v[i] - cnt * abs(p[i] - p[s]) + dp(s, i, cnt - , ));
}
else { //pos==e
for(int i = ; i < s; i++) //e->i //i在区间的左边
ans = max(ans, v[i] - cnt * abs(p[i] - p[e]) + dp(i, e, cnt - , ));
for(int i = e + ; i < n; i++) //e->i //i在区间的右边
ans = max(ans, v[i] - cnt * abs(p[i] - p[e]) + dp(s, i, cnt - , ));
}
return ans;
} //DP要枚举出所有具有最优可能性的情况 不能遗漏否则问题就可能不是最优 int main() {
int T;
scanf("%d",&T);
memset(vis, , sizeof(vis));
for(kase = ; kase <= T; kase++) {
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%d", &p[i]); //位置[]
for(int i = ; i < n; i++) scanf("%d", &v[i]); //原利[] int ans = ;
for(int k = ; k <= n; k++) //枚举送餐人数
for(int i = ; i < n; i++) //枚举给送餐的第一个人
ans = max(ans, v[i] - k * abs(p[i]) + dp(i, i, k - , )); //枚举比较 make_max
printf("%d\n",ans);
}
return ;
}

【暑假】[深入动态规划]UVa 1628 Pizza Delivery的更多相关文章

  1. Pizza Delivery

    Pizza Delivery 时间限制: 2 Sec  内存限制: 128 MB 题目描述 Alyssa is a college student, living in New Tsukuba Cit ...

  2. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  3. 【暑假】[深入动态规划]UVa 12170 Easy Climb

    UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路:  引别人一 ...

  4. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  5. 【暑假】[深入动态规划]UVa 10618 Fun Game

    UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路:   一圈人围坐 ...

  6. 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall

    UVa 10618 Fixing the Great Wall 题目:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...

  7. 【暑假】[深入动态规划]UVa 1627 Team them up!

    UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Forma ...

  8. 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection

    UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...

  9. 【暑假】[深入动态规划]UVa 1412 Fund Management

    UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS   Memory Limit: Unknown   ...

随机推荐

  1. jquery判断对象是否获得焦点

    var isFocus=$("#tRow").is(":focus"); if(true==isFocus){ alert("focus") ...

  2. string.Equals 比较2个字符串是否相同忽略大小写

    bool res = string.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase)

  3. (转)struts2.0配置文件、常量配置详解

    一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...

  4. python datetime笔记

    python datetime笔记 http://mint-green.diandian.com/post/2011-09-09/4892024 获取当前时间,并通过字符串输出. 格式为:%Y-%m- ...

  5. linux下添加PATH环境变量

    添加PATH环境变量,第1种方法:[root@lx_web_s1 ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH 再次查看: [root@lx ...

  6. HDU1054Strategic Game(最小顶点覆盖数)

    我们来先了解一下什么是最小顶点覆盖: 图G的顶点覆盖是一个顶点集合V,使得G中的每一条边都接触V中的至少一个顶点.我们称集合V覆盖了G的边.最小顶点覆盖是用最少的顶点来覆盖所有的边.顶点覆盖数是最小顶 ...

  7. Apache Wamp WampServer 配置多端口 多站点 虚拟目录

    第一步:配置Apache 的 httpd.conf #Listen 0.0.0.0:80Listen 80Listen 81 第二步:开启虚拟站点 所属文件:httpd.conf #Virtual h ...

  8. C# winform DataGridView

    C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如 ...

  9. Windows下Vim设置

    进入安装目录,找到_vimrc文件,用文本编辑器打开,在前面加入下述内容 设置中文支持 " 设置编码自动识别, 中文引号显示 " set fileencodings=utf-8,c ...

  10. 各种html5 的 polyfill

    https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills 配合 Modernizr