题解:
由于
n≤10
n
≤
10
, 所以显然的解法是状态压缩
dp
d
p
,
dp(i,mask)
d
p
(
i
,
m
a
s
k
)
表示走到第
i
i
个河岸,船上的人集合是的最少时间花费。
转移方程:
dp(i,mask)=minmask′{dp(i−1,mask′)+trans(mask,mask′)+costi(mask)}
d
p
(
i
,
m
a
s
k
)
=
m
i
n
m
a
s
k
′
{
d
p
(
i
−
1
,
m
a
s
k
′
)
+
t
r
a
n
s
(
m
a
s
k
,
m
a
s
k
′
)
+
c
o
s
t
i
(
m
a
s
k
)
}
时间复杂度是
O(22nm)
O
(
2
2
n
m
)
的,如果常数写的很小能够AC。
#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define ULL ull
#define mp make_pair
#define pii pair<int,int>
#define piii pair<int, pii >
#define pll pair <ll,ll>
#define pb push_back
#define big 20160116
#define INF 2147483647
#define pq priority_queue
using namespace std;
inline int read(){
int 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;
}
namespace Mymath{
LL qp(LL x,LL p,LL mod){
LL ans=1;
while (p){
if (p&1) ans=ans*x%mod;
x=x*x%mod;
p>>=1;
}
return ans;
}
LL inv(LL x,LL mod){
return qp(x,mod-2,mod);
}
LL C(LL N,LL K,LL fact[],LL mod){
return fact[N]*inv(fact[K],mod)%mod*inv(fact[N-K],mod)%mod;
}
template <typename Tp> Tp gcd(Tp A,Tp B){
if (B==0) return A;
return gcd(B,A%B);
}
template <typename Tp> Tp lcm(Tp A,Tp B){
return A*B/gcd(A,B);
}
};
namespace fwt{
using namespace Mymath;
void FWT(int a[],int n,LL mod)
{
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
//xor:a[i+j]=x+y,a[i+j+d]=x-y;
//and:a[i+j]=x+y;
//or:a[i+j+d]=x+y;
}
}
void UFWT(int a[],int n,LL mod)
{
LL rev=inv(2,mod);
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
//xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
//and:a[i+j]=x-y;
//or:a[i+j+d]=y-x;
}
}
void solve(int a[],int b[],int n,LL mod)
{
FWT(a,n,mod);
FWT(b,n,mod);
for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
UFWT(a,n,mod);
}
};
const int Maxn=1035;
int dp[Maxn][Maxn];
int w[Maxn],t[Maxn],s[Maxn];
int c[Maxn],d[Maxn],D[Maxn];
int W[Maxn];
int n,m;
void R(int i){
for (int j=0;j<n;j++){
for (int mask=0;mask<(1<<n);mask++){
dp[i][mask^(1<<j)]=min(dp[i][mask^(1<<j)],dp[i][mask]+s[j]);
}
}
}
int C[Maxn],S[Maxn];
int main(){
//int n,m;
n=read();m=read();
for (int i=0;i<n;i++){
w[i]=read();t[i]=read();s[i]=read();
}
for(int i=0;i<(1<<n);i++){
for (int j=0;j<n;j++){
if (i>>j&1) W[i]+=w[j],S[i]+=s[j];
}
//cout<<i<<' '<<W[i]<<endl;
}
for(int i=0;i<(1<<n);i++){
for (int j=0;j<n;j++){
if (i>>j&1) C[i]=max(C[i],t[j]);
}
//cout<<i<<' '<<W[i]<<endl;
}
for (int i=1;i<=m;i++){
c[i]=read();D[i]=read();d[i]=read();
}
for (int i=0;i<Maxn;i++){
for (int j=0;j<Maxn;j++){
dp[i][j]=1e9;
}
}
int Al=1<<n;
Al--;
//for (int i=0;i<1)
dp[0][0]=0;
int lim=1<<n;
for (int i=1;i<=m;i++){
int ii=i-1;
if (n>=3){
for (register int j=0;j<lim;++j){
for (register int k=0;k<lim;k+=2){
(dp[ii][j]+S[k^j]<dp[ii][k])?dp[ii][k]=dp[ii][j]+S[k^j]:1;
(dp[ii][j]+S[k^j^1]<dp[ii][k^1])?dp[ii][k^1]=dp[ii][j]+S[k^j^1]:1;
}
}
}
else{
for (register int j=0;j<lim;++j){
for (register int k=0;k<lim;++k){
if (dp[ii][j]+S[k^j]<dp[ii][k]) dp[ii][k]=dp[ii][j]+S[k^j];
}
}
}
for (int j=1;j<(1<<n);j++){
if (W[j]>c[i]){
//cerr<<j<<endl;
dp[i][j]=dp[i-1][j]+max(D[i],C[Al^j]);
}
else{
dp[i][j]=dp[i-1][j]+max(d[i],C[Al^j]);
}
}
}
int ii=m;
for (register int j=0;j<lim;++j){
for (register int k=0;k<lim;++k){
if (dp[ii][j]+S[k^j]<dp[ii][k]) dp[ii][k]=dp[ii][j]+S[k^j];
}
}
printf("%d\n",dp[m][0]);
return 0;
}
考虑优化状态转移。
发现在每个河岸处第
i
i
个人和第人的状态改变是独立的。
所以我们把每个人的状态改变分开考虑,转移的复杂度就变成
O(n)
O
(
n
)
了。
总时间复杂度
O(2nnm)
O
(
2
n
n
m
)
#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define ULL ull
#define mp make_pair
#define pii pair<int,int>
#define piii pair<int, pii >
#define pll pair <ll,ll>
#define pb push_back
#define big 20160116
#define INF 2147483647
#define pq priority_queue
using namespace std;
inline int read(){
int 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;
}
namespace Mymath{
LL qp(LL x,LL p,LL mod){
LL ans=1;
while (p){
if (p&1) ans=ans*x%mod;
x=x*x%mod;
p>>=1;
}
return ans;
}
LL inv(LL x,LL mod){
return qp(x,mod-2,mod);
}
LL C(LL N,LL K,LL fact[],LL mod){
return fact[N]*inv(fact[K],mod)%mod*inv(fact[N-K],mod)%mod;
}
template <typename Tp> Tp gcd(Tp A,Tp B){
if (B==0) return A;
return gcd(B,A%B);
}
template <typename Tp> Tp lcm(Tp A,Tp B){
return A*B/gcd(A,B);
}
};
namespace fwt{
using namespace Mymath;
void FWT(int a[],int n,LL mod)
{
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
//xor:a[i+j]=x+y,a[i+j+d]=x-y;
//and:a[i+j]=x+y;
//or:a[i+j+d]=x+y;
}
}
void UFWT(int a[],int n,LL mod)
{
LL rev=inv(2,mod);
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
//xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
//and:a[i+j]=x-y;
//or:a[i+j+d]=y-x;
}
}
void solve(int a[],int b[],int n,LL mod)
{
FWT(a,n,mod);
FWT(b,n,mod);
for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
UFWT(a,n,mod);
}
};
const int Maxn=1035;
int dp[Maxn][Maxn];
int w[Maxn],t[Maxn],s[Maxn];
int c[Maxn],d[Maxn],D[Maxn];
int W[Maxn];
int n,m;
void R(int i){
for (int j=0;j<n;j++){
for (int mask=0;mask<(1<<n);mask++){
dp[i][mask^(1<<j)]=min(dp[i][mask^(1<<j)],dp[i][mask]+s[j]);
}
}
}
int C[Maxn];
int main(){
//int n,m;
n=read();m=read();
for (int i=0;i<n;i++){
w[i]=read();t[i]=read();s[i]=read();
}
for(int i=0;i<(1<<n);i++){
for (int j=0;j<n;j++){
if (i>>j&1) W[i]+=w[j];
}
//cout<<i<<' '<<W[i]<<endl;
}
for(int i=0;i<(1<<n);i++){
for (int j=0;j<n;j++){
if (i>>j&1) C[i]=max(C[i],t[j]);
}
//cout<<i<<' '<<W[i]<<endl;
}
for (int i=1;i<=m;i++){
c[i]=read();D[i]=read();d[i]=read();
}
for (int i=0;i<Maxn;i++){
for (int j=0;j<Maxn;j++){
dp[i][j]=1e9;
}
}
int Al=1<<n;
Al--;
//for (int i=0;i<1)
dp[0][0]=0;
for (int i=1;i<=m;i++){
R(i-1);
//dp[i-1][0]=1e9;
//for (int j=0;j<(1<<n);j++) cout<<dp[i-1][j]<<' ';
//cout<<endl;
for (int j=1;j<(1<<n);j++){
if (W[j]>c[i]){
//cerr<<j<<endl;
dp[i][j]=dp[i-1][j]+max(D[i],C[Al^j]);
}
else{
dp[i][j]=dp[i-1][j]+max(d[i],C[Al^j]);
}
}
}
R(m);
printf("%d\n",dp[m][0]);
return 0;