題解:
由於 , 所以顯然的解法是狀態壓縮 , 表示走到第 個河岸,船上的人集合是 的最少時間花費。
轉移方程:
時間複雜度是 的,如果常數寫的很小能夠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;
}
考慮優化狀態轉移。
發現在每個河岸處第 個人和第 人的狀態改變是獨立的。
所以我們把每個人的狀態改變分開考慮,轉移的複雜度就變成 了。
總時間複雜度
#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;