题意与分析(CodeForces 580D)
代码
#include <bits/stdc++.h>
#define MP make_pair
#define PB emplace_back
#define fi first
#define se second
#define ZERO(x) memset((x), 0, sizeof(x))
#define ALL(x) (x).begin(),(x).end()
#define rep(i, a, b) for (repType i = (a); i <= (b); ++i)
#define per(i, a, b) for (repType i = (a); i >= (b); --i)
#define MS(x,y) memset(x,y,sizeof(x))
#define QUICKIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
using ll=long long;
using repType=ll;
ll dp[25][1000005];
ll taste[25];
ll extra[25][25];
int n,m,k;
ll solve(int last, int stat) // stat after eating up last
{
//cout<<last<<" "<<stat<<endl;
if(dp[last][stat]!=-1) return dp[last][stat];
else
{
ll ans=0;
int val=(stat-(1<<(last-1)));
rep(i,1,n)
{
if(val&(1<<(i-1)))
{
ans=max(ans,solve(i,val)+extra[i][last]+taste[last]);
}
}
//cout<<" "<<last<<": "<<stat<<" "<<ans<<endl;
return dp[last][stat]=ans;
}
}
int main()
{
MS(dp,-1);
cin>>n>>m>>k;
rep(i,1,n)
{
cin>>taste[i];
dp[i][1<<(i-1)]=taste[i];
}
/*
rep(i,1,n)
{
rep(j,0,((1<<n)-1))
cout<<dp[i][j]<<" ";
cout<<endl;
}
*/
rep(i,1,k)
{
int x,y,c;
cin>>x>>y>>c;
extra[x][y]=c;
}
ll ans=0;
rep(i,1,n) dp[i][(1<<n)-1]=solve(i,(1<<n)-1);
rep(i,1,n)
{
rep(j,0,((1<<n)-1))
{
if(__builtin_popcount(j)==m)
{
ans=max(ans,dp[i][j]);
}
//cout<<dp[i][j]<<" ";
}
//cout<<endl;
}
cout<<ans<<endl;
}