跳到正文

个人记录

ak

wangsy2014约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年2月5日
更新
2026年2月5日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1
C++
T1:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=25,M=(1<<20)+5;
int n,a[N][N],dp[M][N];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cin>>a[i][j];
		}
	}
	memset(dp,0x3f,sizeof(dp));
	dp[1][0]=0;
	for(int i=1;i<(1<<n);i++)
	{
		for(int j=0;j<n;j++)
		{
			if(!((i>>j)&1))
			{
				continue;
			}
			for(int k=0;k<n;k++)
			{
				if(((i^(1<<j))>>k)&1)
				{
					dp[i][j]=min(dp[i][j],dp[i^(1<<j)][k]+a[k][j]);
				}
			 }
		}
	}
	cout<<dp[(1<<n)-1][n-1];
	return 0;
}
T2:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=25,M=(1<<16)+5;
int a,b,t[N],w[N],t2[M],w2[M],dp[M];
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cin>>a>>b;
	for(int i=1;i<=b;i++)
	{
		cin>>t[i]>>w[i];
	}
	for(int i=0;i<(1<<b);i++)
	{
		for(int j=1;j<=b;j++)
		{
			if(i&(1<<(j-1)))
			{
				t2[i]=max(t2[i],t[j]);
				w2[i]+=w[j];
			}
		}
	}
	memset(dp,0x3f,sizeof dp);
	dp[0]=0;
	for(int i=0;i<(1<<b);i++)
	{
		for(int j=i;true;j=i&(j-1))
		{
			if(w2[i^j]<=a)
			{
				dp[i]=min(dp[i],dp[j]+t2[i^j]);
			}
			if(!j)
			{
				break;
			 }
		}
	}
	cout<<dp[(1<<b)-1];
	return 0;
}
原文