CF580D Kefa and Dishes (状压DP)

枚举最后食物

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;
#define int long long
int a[19], val[19][19], f[(1 << 18) + 7][19];
#undef int
int main(){
#define int long long
	int n, m, k;
    io >> n >> m >> k;
    R(i,1,n)
        io >> a[i];
        
    R(i,1,k){
    	int x, y;
    	io >> x >> y;
    	io >> val[x][y];
    }
    R(i,1,n)
        f[1 << (i - 1)][i]=a[i];

	int ans = 0;
    R(s,0,(1 << n) - 1){
        int tot=0;
        R(i,1,n)
			if(s & (1 << (i - 1))){
	            ++tot;
	            R(j,1,n)
					if(!(s & (1 << (j - 1)))){
						f[s | (1 << (j - 1))][j] = Max(f[s | (1 << (j - 1))][j] , f[s][i] + a[j] + val[i][j]);
					}
	        }
	        if(tot == m)
	        	R(i,1,n)
					if(s & (1 << (i - 1)))
	            		ans = Max(ans , f[s][i]);
    }
    
    printf("%lld", ans);

    return 0;
}

posted @ 2019-07-25 21:55  邱涵的秘密基地  阅读(130)  评论(0编辑  收藏  举报