codeforces 321# D. Kefa and Dishes (状态压缩DP)
2015-09-24 16:16
381 查看
题目:http://codeforces.com/contest/580/problem/D
题意:有n种菜(每一种菜有一个满意值ai>=0),你准备吃m种,每种一次。但是如果你按某种规则吃两种菜的话会增加额外的满意值,比如规则(xi yi ci)就是你先吃第xi个菜,然后马上吃第yi个菜,那么你就会额外增加ci点满意值。有k个这样的规则,问你吃m种菜后的最大满意值是多少。
分析:定义dp[i][j],i是bitmask,第x位为1代表选了第x种菜,j代表最后一次选的菜的下标,dp[i][j]是当前状态为(bitmask,lastnum)的剩余状态的最优解。然后记忆化搜索就行了。与之前习惯性写的含义不一样,之前写的是到达当前状态所选物品的最优解,这样肯定会错,因为和已选物品的先后顺序有关,假如求出dp[b][l],下次在到达这个状态(b,l)就直接返回了先前的那个可能更差的解。
错误代码:
D. Kefa and Dishes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that
he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.
Kefa knows that the i-th dish gives him ai units
of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating
food of the following type — if he eats dish x exactly before dish y (there
should be no other dishes between x and y),
then his satisfaction level raises by c.
Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!
Input
The first line of the input contains three space-separated numbers, n, m and k (1 ≤ m ≤ n ≤ 18, 0 ≤ k ≤ n * (n - 1))
— the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.
The second line contains n space-separated numbers ai,
(0 ≤ ai ≤ 109)
— the satisfaction he gets from the i-th dish.
Next k lines contain the rules. The i-th
rule is described by the three numbers xi, yi and ci (1 ≤ xi, yi ≤ n, 0 ≤ ci ≤ 109).
That means that if you eat dish xi right
before dish yi,
then the Kefa's satisfaction increases by ci.
It is guaranteed that there are no such pairs of indexesi and j (1 ≤ i < j ≤ k),
that xi = xj and yi = yj.
Output
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.
Sample test(s)
input
output
input
output
Note
In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.
In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5.
题意:有n种菜(每一种菜有一个满意值ai>=0),你准备吃m种,每种一次。但是如果你按某种规则吃两种菜的话会增加额外的满意值,比如规则(xi yi ci)就是你先吃第xi个菜,然后马上吃第yi个菜,那么你就会额外增加ci点满意值。有k个这样的规则,问你吃m种菜后的最大满意值是多少。
分析:定义dp[i][j],i是bitmask,第x位为1代表选了第x种菜,j代表最后一次选的菜的下标,dp[i][j]是当前状态为(bitmask,lastnum)的剩余状态的最优解。然后记忆化搜索就行了。与之前习惯性写的含义不一样,之前写的是到达当前状态所选物品的最优解,这样肯定会错,因为和已选物品的先后顺序有关,假如求出dp[b][l],下次在到达这个状态(b,l)就直接返回了先前的那个可能更差的解。
错误代码:
long long DFS(int bitmask,int lastnum,long long val) { int c=cal(bitmask); if(c>=m) { if(val>ans) ans=val; return val; } if(dp[bitmask][lastnum]!=-1) return dp[bitmask][lastnum]; long long ret=-1; for(int i=0;i<n;i++) { if(bitmask&(1<<i)) continue ; ret=max(ret,DFS(bitmask^(1<<i),i,val+v[i]+buf[lastnum][i])); } return dp[bitmask][lastnum]=ret; }ac代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; int n,m,k; long long v[20],buf[20][20]; long long ans,dp[1<<19][20]; long long DFS(int bitmask,int lastnum,int p) { if(dp[bitmask][lastnum]!=-1) return dp[bitmask][lastnum]; if(p==m) return 0; long long ret=0; for(int i=0;i<n;i++) { if(bitmask&(1<<i)) continue ; ret=max(ret,DFS(bitmask^(1<<i),i,p+1)+v[i]+buf[lastnum][i]); } return dp[bitmask][lastnum]=ret; } int main() { int i,j,x,y,vl; scanf("%d%d%d",&n,&m,&k); for(i=0;i<n;i++) scanf("%lld",&v[i]); memset(buf,0,sizeof(buf)); while(k--) { scanf("%d%d%d",&x,&y,&vl); buf[x-1][y-1]=vl; } ans=-1; memset(dp,-1,sizeof(dp)); printf("%lld\n",DFS(0,19,0)); return 0; }
D. Kefa and Dishes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that
he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.
Kefa knows that the i-th dish gives him ai units
of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating
food of the following type — if he eats dish x exactly before dish y (there
should be no other dishes between x and y),
then his satisfaction level raises by c.
Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!
Input
The first line of the input contains three space-separated numbers, n, m and k (1 ≤ m ≤ n ≤ 18, 0 ≤ k ≤ n * (n - 1))
— the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.
The second line contains n space-separated numbers ai,
(0 ≤ ai ≤ 109)
— the satisfaction he gets from the i-th dish.
Next k lines contain the rules. The i-th
rule is described by the three numbers xi, yi and ci (1 ≤ xi, yi ≤ n, 0 ≤ ci ≤ 109).
That means that if you eat dish xi right
before dish yi,
then the Kefa's satisfaction increases by ci.
It is guaranteed that there are no such pairs of indexesi and j (1 ≤ i < j ≤ k),
that xi = xj and yi = yj.
Output
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.
Sample test(s)
input
2 2 1 1 1 2 1 1
output
3
input
4 3 2 1 2 3 4 2 1 5 3 4 2
output
12
Note
In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.
In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5.
相关文章推荐
- Cognos Schedules does not work
- 布隆过滤器
- TexturePacker压缩png的命令
- 数独
- phpExcel中文帮助手册之常用功能指南
- Android事件的传递机制
- Shell脚本编程小结
- 数据结构之链表定义及基本操作实现
- HttpHandler与HttpModule的用处与区别
- 使用Notepad++的XML Tools插件格式化XML文件
- 推荐几本介绍Android Studio开发的好书
- iOS扫描二维码的实现
- setsockopt的作用
- 友盟数据统计、消息推送、社会化分享
- window下编译QT5出现错误:Cannot find file: D:\用户目录\Documents\ThreadClass\ThreadClass.pro.
- kafka命令记录
- 理解操作系统的sleep函数
- MFC中改变对话框背景的几个消息函数OnEraseBkgnd、 OnPaint、 OnCtlColor的调用顺序
- 面试必须get的技能点
- 在ASP.Net和IIS中删除不必要的HTTP响应头