显示代码纯文本
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<iostream>
using namespace std;
int f[1050000],a[30][30],b[30][30],c[30];
int map[30][30];
int ans,n,m;
int lowbit(int x)
{
return x&(-x);
}
int getmany(int x)
{
int tt=0;
while (x>0)
{
tt++;
x=x-lowbit(x);
}
return tt;
}
bool check(int x,int y)
{
x=x>>(y-1);
if ((x&1)==0) return false;
else return true;
}
int main()
{
freopen("deca.in","r",stdin);
freopen("deca.out","w",stdout);
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for (int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
c[x]++;
a[x][c[x]]=y;
b[x][c[x]]=z;
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
}
}
memset(f,0,sizeof(f));
for (int i=1;i<=((1<<n)-1);i++)
{
int ans=0;
int x=getmany(i);
for (int j=1;j<=n;j++)
{
if (check(i,j))
{
int t1=0;
int t2=0;//奖励
t1=f[i^(1<<(j-1))]+map[j][x];
for (int k=1;k<=c[x];k++)
{
if (t1>=a[x][k]) t2=t2+b[x][k];
}
ans=max(ans,t1+t2);
}
}
f[i]=ans;
}
printf("%d",f[(1<<n)-1]);
}