Codeforces Round #321 (Div. 2) D. Kefa and Dishes 位 状态压缩 dp
2015-10-01 21:52
429 查看
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 indexes i 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个菜,选出m个菜,其中,菜满足一定的前后顺序,就可以增加满意度。要求最大的满意度。
用dp来做,用一个int的低位,0 1表示,某个菜是否已选,这样就可以用一个int来表示状态了。
dp[i][j]表示,当前状态是i,最后一个菜是j时的最大值。由于,只和最后一个菜相关,所以可以这样做。
状态转移就是,dp[i][j] 加上每k个菜,可以得到dp[i^(1<<k)][k].
复杂度为,o(2^n * n ^2);
#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int t,n,m,k,land
,p
,a,b,c;
ll dp[1<<19][20];
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(S2(n,m)!=EOF)
{
S(k);
fill(land,0);
FI(n) S(p[i]);
FI(k){
S2(a,b);
S(c);
a--;b--;
land[a][b] = c;
}
int s = (1<<n);
for(int i = 0;i<s;i++){
for(int j = 0;j<n;j++){
dp[i][j] = 0;
}
}
for(int j = 0;j<n;j++){
dp[1<<j][j] = p[j];
}
ll maxx = 0;
for(int i = 0;i<s;i++)
for(int j = 0;j<n;j++){
if((i & (1 << j))){
for(int k = 0;k<n;k++)
if(j != k && !(i & (1 << k))){
//printf(" %d %d %d %d %d \n",i,j,i^(1<<k),k,land[j][k]);
//cout<<dp[i][j]<<" "<<dp[i^(1<<k)][k]<<endl;
dp[i^(1<<k)][k] = max(dp[i^(1<<k)][k],dp[i][j] + (ll)p[k] + (ll)land[j][k]);
}
}
int ti = i,num = 0;
while(ti){
if(ti & 1) num++;
ti>>=1;
}
if(num == m)
maxx = max(maxx,dp[i][j]);
}
printf("%lld\n",maxx);
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
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 indexes i 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.
题意,要求,有n个菜,选出m个菜,其中,菜满足一定的前后顺序,就可以增加满意度。要求最大的满意度。
用dp来做,用一个int的低位,0 1表示,某个菜是否已选,这样就可以用一个int来表示状态了。
dp[i][j]表示,当前状态是i,最后一个菜是j时的最大值。由于,只和最后一个菜相关,所以可以这样做。
状态转移就是,dp[i][j] 加上每k个菜,可以得到dp[i^(1<<k)][k].
复杂度为,o(2^n * n ^2);
#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int t,n,m,k,land
,p
,a,b,c;
ll dp[1<<19][20];
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(S2(n,m)!=EOF)
{
S(k);
fill(land,0);
FI(n) S(p[i]);
FI(k){
S2(a,b);
S(c);
a--;b--;
land[a][b] = c;
}
int s = (1<<n);
for(int i = 0;i<s;i++){
for(int j = 0;j<n;j++){
dp[i][j] = 0;
}
}
for(int j = 0;j<n;j++){
dp[1<<j][j] = p[j];
}
ll maxx = 0;
for(int i = 0;i<s;i++)
for(int j = 0;j<n;j++){
if((i & (1 << j))){
for(int k = 0;k<n;k++)
if(j != k && !(i & (1 << k))){
//printf(" %d %d %d %d %d \n",i,j,i^(1<<k),k,land[j][k]);
//cout<<dp[i][j]<<" "<<dp[i^(1<<k)][k]<<endl;
dp[i^(1<<k)][k] = max(dp[i^(1<<k)][k],dp[i][j] + (ll)p[k] + (ll)land[j][k]);
}
}
int ti = i,num = 0;
while(ti){
if(ti & 1) num++;
ti>>=1;
}
if(num == m)
maxx = max(maxx,dp[i][j]);
}
printf("%lld\n",maxx);
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
相关文章推荐
- Centos VPS 创建多个网站 一
- 我们为什么选择acm(个人观点)
- android studio在模拟器上的中文乱码问题解决方法
- hdu 5491 The Next(位运算+贪心)
- 第一层第三题:黑色星期五
- python3.4-小爬虫
- QT 布局管理器的嵌套使用
- Uva437 The Tower of Babylon
- 《胡雪岩 5· 烟消云散》—— 读书笔记
- Codeforces 557E Ann and Half-Palindrome (Trie树)
- 机器学习(九)—FP-growth算法
- JavaAPI_3
- C++学习书籍
- Ubuntu系统安装
- 2015.10.01
- ZetCode PyQt4 tutorial signals and slots
- xpcall 安全调用
- CG写实数字插画绘画视频教程
- CentOS系统如何与VirtualBox虚拟机共享文件夹
- win10下硬盘安装(更新)win10