30pts 状压求助
查看原帖
30pts 状压求助
195331
Mine_KingCattleya楼主2021/11/23 22:09

Rt。不知道为啥 30pts,剩下的全 WA。求大佬帮忙调调

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lowbit(x) (x&-x)
using namespace std;
int n,m,dp[2<<18][20],g[20][20];
int log(int x)
{
	int res=0;
	while(x) x>>=1,res++;
	return res;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		u++,v++;
		g[u][v]=w;
	}
	memset(dp,0x80,sizeof(dp));
	dp[1][1]=0;
	for(int S=3;S<(1<<n);S+=2)
		for(int S1=S;S1;S1-=lowbit(S1))
		{
			int u=log(lowbit(S1));
			for(int S2=S;S2;S2-=lowbit(S2))
			{
				// if(S==3&&u==1) printf("%d\n",lowbit(S2));
				int v=log(lowbit(S2));
				// if(u==2&&v==3&&S==7) printf("%d\n",);
				if(g[u][v]==0) continue;
				dp[S][v]=max(dp[S][v],dp[S^(1<<(v-1))][u]+g[u][v]);
			}
		}
	int ans=0;
	for(int S=(1<<(n-1))+1;S<(1<<n);S+=2)
		ans=max(ans,dp[S][n]);
	// printf("debug:%d\n",dp[3][2]);
	printf("%d",ans);
	return 0;
}
2021/11/23 22:09
加载中...