Most Powerful(状压DP水题)

题目链接:

https://ac.nowcoder.com/acm/problem/15832

题目大意:

自己翻译,注意每次碰撞是两个中的一个消失,并不是两个都消失

具体思路:

dp[i]表示i这个状态最大的能量是多少,三重for循环枚举

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 # define inf 0x3f3f3f3f
 5 # define LL_inf (1ll<<60)
 6 const int maxn = 2e5+100;
 7 int dp[1024+100];
 8 int a[15][15];
 9 int main()
10 {
11     int n;
12     while(~scanf("%d",&n)&&n)
13     {
14         memset(dp,0,sizeof(dp));
15         for(int i=0; i<n; i++)
16         {
17             for(int j=0; j<n; j++)
18             {
19                 scanf("%d",&a[i][j]);
20             }
21         }
22         int maxstate=(1<<n)-1;
23         int maxx=0;
24         for(int i=0; i<=maxstate; i++)
25         {
26             for(int j=0; j<n; j++)
27             {
28                 for(int k=0; k<n; k++)
29                 {
30                     if(k==j)
31                         continue;
32                     if((i&(1<<j))==0 && (i&(1<<k))==0)
33                         dp[i^(1<<j)]=max(dp[i^(1<<j)],dp[i]+a[k][j]);
34                     maxx=max(maxx,dp[i^(1<<j)]);
35                 }
36             }
37         }
38         printf("%d\n",maxx);
39     }
40     return 0;
41 }

acm生涯应该就到此结束了

猜你喜欢

转载自www.cnblogs.com/letlifestop/p/11098159.html