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


    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
    ",dfs(0,n+1,0));
    }
  • 相关阅读:
    xCode中怎样保存自己的代码块
    2015-03-13---抽象工厂(附代码),
    java nio 缓冲区(一)
    MFC获取各个窗体(体)之间的指针(对象)
    自己动手写神经网络,自己真的能够动手写神经网络嘛?
    Android招財进宝手势password的实现
    QQ三方登录
    UVA 10561
    Vagi单点登录1.0
    《反脆弱》:软件业现成的鲁棒性(Robust)换了个说法变成了作者的发明,按作者的理论推导出许多可笑愚蠢的原则来
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4831560.html
Copyright © 2020-2023  润新知