标签:
2 3 3 2 2 2 1 2 2 3 3 1 4 6 1 2 3 4 1 2 1 3 1 4 2 3 2 4 3 4
22 3 69 1
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 14; 5 bool arc[maxn][maxn]; 6 int dp[1<<maxn][maxn][maxn],val[20],n,m; 7 LL cnt[1<<maxn][maxn][maxn]; 8 int main() { 9 int cs; 10 scanf("%d",&cs); 11 while(cs--) { 12 scanf("%d %d",&n,&m); 13 memset(arc,false,sizeof arc); 14 for(int i = 0; i < n; ++i) scanf("%d",val+i); 15 for(int u,v, i = 0; i < m; ++i) { 16 scanf("%d %d",&u,&v); 17 arc[u-1][v-1] = arc[v-1][u-1] = true; 18 } 19 if(n == 1) { 20 printf("%d 1\n",val[0]); 21 continue; 22 } 23 memset(dp,-1,sizeof dp); 24 memset(cnt,0,sizeof cnt); 25 for(int i = 0; i < n; ++i) 26 for(int j = 0; j < n; ++j) 27 if(i != j && arc[i][j]) { 28 dp[(1<<i)|(1<<j)][i][j] = val[i] + val[j] + val[i]*val[j]; 29 cnt[(1<<i)|(1<<j)][i][j] = 1; 30 } 31 for(int i = 0; i < (1<<n); ++i) { 32 for(int j = 0; j < n; ++j) { 33 if(i&(1<<j)) { 34 for(int k = 0; k < n; ++k) { 35 if(j != k && (i&(1<<k)) && arc[j][k] && dp[i][j][k] != -1) { 36 for(int t = 0; t < n; ++t) { 37 if((i&(1<<t)) == 0 && arc[k][t] && j != t && k != t) { 38 int tmp = dp[i][j][k] + val[t] + val[k]*val[t]; 39 if(arc[j][t]) tmp += val[j]*val[k]*val[t]; 40 if(dp[i|(1<<t)][k][t] == tmp) 41 cnt[i|(1<<t)][k][t] += cnt[i][j][k]; 42 else if(dp[i|(1<<t)][k][t] < tmp) { 43 dp[i|(1<<t)][k][t] = tmp; 44 cnt[i|(1<<t)][k][t] = cnt[i][j][k]; 45 } 46 } 47 } 48 } 49 } 50 } 51 } 52 } 53 int ret = 0; 54 LL ret2 = 0; 55 for(int i = 0; i < n; ++i) 56 for(int j = 0; j < n; ++j) 57 if(i != j && arc[i][j]) { 58 if(ret < dp[(1<<n)-1][i][j]) { 59 ret = dp[(1<<n)-1][i][j]; 60 ret2 = cnt[(1<<n)-1][i][j]; 61 } else if(ret == dp[(1<<n)-1][i][j]) 62 ret2 += cnt[(1<<n)-1][i][j]; 63 } 64 printf("%d %I64d\n",ret,ret2>>1); 65 } 66 return 0; 67 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4550255.html