Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

本文解析了Codeforces上D.KefaandDishes问题,这是一道涉及状态压缩动态规划的问题,需要通过记忆化搜索来解决。文章详细介绍了如何利用状态压缩和动态规划来最大化角色Kefa在餐厅用餐时的满意度。

D. Kefa and Dishes

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/580/problem/D

Description

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, nm 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 xiyi and ci (1 ≤ xi, yi ≤ n0 ≤ 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 Input

2 2 1
1 1
2 1 1

Sample Output

3

HINT

 

题意

一个人有n道菜,然后要点m道菜,每道菜有一个美味程度

然后给你了很多个关系,表示如果x刚好在y前面做的话,他的美味程度就会增加c

题解:

裸的状压dp,记忆化搜索也可以跑

看到这个数据范围就是状压= =

代码:

//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::ssecondnc_with_stdio(0);cin.tie(0)
#define maxn 20
#define mod 1000000007
#define eps 1e-9
#define PI acos(-1)
const double EP  = 1E-10 ;
int Num;
//const int inf=0first7fffffff;
const ll inf=999999999;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//*************************************************************************************


ll dp[1<<18][20];
ll a[maxn],M[maxn][maxn];
int n,m,k;
ll dfs(int x,int y,int z)
{
    if( dp[x][y] !=-1 ) return dp[x][y];
    dp[x][y]=0;
    if( z==m )return dp[x][y];
    for(int i=0;i<n;i++)
    {
        if( ((x >> i) & 1) == 0)
        {
            dp[x][y]=max( dp[x][y], dfs( x | (1<<i) , i , z+1 ) + M[y][i] + a[i] );
        }
    }
    return dp[x][y];
}
int main()
{
    memset(dp,-1,sizeof(dp));
    n=read(),m=read(),k=read();
    for(int i=0;i<n;i++)
        a[i]=read();
    for(int i=0;i<k;i++)
    {
        int x=read(),y=read();
        ll c=read();
        M[x-1][y-1]+=c;
    }
    printf("%lld\n",dfs(0,n+1,0));
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值