抄书
#include<stdio.h> #include<iostream> #include<string.h> #define maxn 13 #define maxstatus 1<<13 using namespace std; typedef long long LL ; LL dp[maxstatus][maxn][maxn]; LL ways[maxstatus][maxn][maxn]; int value[maxn]; bool link[maxn][maxn]; int nislands; LL maxvalue; LL maxways; void init() { int nbridges; int i;int row;int col; scanf("%d%d",&nislands,&nbridges); for(int i=0;i<nislands;i++){ scanf("%d",value+i); } if(nislands==1) return ; memset(dp,-1,sizeof(dp)); memset(ways,0,sizeof(ways)); memset(link,0,sizeof(link)); for(int i=0;i<nbridges;i++){ scanf("%d%d",&row,&col); row--;col--; link[row][col]=1;link[col][row]=1; dp[(1<<row)|(1<<col)][row][col]=dp[(1<<col)|(1<<row)][col][row]=value[col]+value[row]+value[col]*value[row]; ways[(1<<row)|(1<<col)][row][col]=ways[(1<<row)|(1<<col)][col][row]=1; } } void Dp() { int s;int i;int j;int k; LL temp; LL nextstatus; if(nislands==1) { maxvalue=value[0];maxways=1; return ; } for(s=0;s<(1<<nislands);s++){ for(i=0;i<nislands;i++){ if(s&(1<<i)){ for(j=0;j<nislands;j++){ if(i!=j&&(s&(1<<j))&&dp[s][i][j]>-1){ for(k=0;k<nislands;k++){ if(!(s&(1<<k))&&link[i][k]==1){ nextstatus=s|(1<<k); temp=dp[s][i][j]+value[k]+value[k]*value[i]; if(link[j][k]==1) temp+=value[k]*value[j]*value[i]; if(dp[nextstatus][k][i]==temp){ ways[nextstatus][k][i]+=ways[s][i][j]; } else if(dp[nextstatus][k][i]<temp){ ways[nextstatus][k][i]=ways[s][i][j]; dp[nextstatus][k][i]=temp; } } } } } } } } maxvalue=-1;maxways=0; s=(1<<nislands)-1; for(int i=0;i<nislands;i++){ for(int j=0;j<nislands;j++){ if(!link[i][j]) continue; if(dp[s][i][j]==maxvalue) maxways+=ways[s][i][j]; else if(dp[s][i][j]>maxvalue){ maxvalue=dp[s][i][j]; maxways=ways[s][i][j]; } } } maxways=maxways/2; } int main() { int i;int cases; scanf("%d",&cases); for(int i=0;i<cases;i++){ init(); Dp(); if(maxvalue==-1) printf("0 0\n"); else cout<<maxvalue<<maxways<<endl; } return 0; }
时间: 2024-10-13 12:36:47