Codeforces1102F Elongated Matrix 【状压DP】
题目分析:
这题瞎搞一个哈密尔顿路,对于起点不同的分开跑就可以过了。
$O(n^3*2^n)$
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 17; 5 const int maxm = 10200; 6 7 int n,m; 8 int a[maxn][maxm],f[maxn][maxn],g[maxn][maxn]; 9 10 int dp[maxn][(1<<16)+5],arr[maxn][(1<<16)+5]; 11 12 void init(){ 13 for(int i=1;i<=n;i++){ 14 for(int j=1;j<=n;j++){ 15 f[i][j] = g[i][j] = 1e9; 16 for(int k=1;k<=m;k++)f[i][j] = min(f[i][j],abs(a[i][k]-a[j][k])); 17 for(int k=1;k<m;k++) g[i][j]=min(g[i][j],abs(a[i][k+1]-a[j][k])); 18 } 19 } 20 } 21 22 queue<pair<int,int> > q; 23 void divide(){ 24 int ans = 0; 25 for(int i=1;i<=n;i++){ 26 memset(dp,0,sizeof(dp)); 27 memset(arr,0,sizeof(arr)); 28 dp[i][1<<i-1] = 1e9; arr[i][1<<i-1] = 1; 29 q.push(make_pair(i,1<<i-1)); 30 while(!q.empty()){ 31 pair<int,int> k = q.front(); q.pop(); 32 for(int j=1;j<=n;j++){ 33 if(k.second&(1<<j-1)) continue; 34 dp[j][k.second|(1<<j-1)] = 35 max(dp[j][k.second|(1<<j-1)],min(dp[k.first][k.second],f[k.first][j])); 36 if(!arr[j][k.second|(1<<j-1)]){ 37 q.push(make_pair(j,k.second|(1<<j-1))); 38 arr[j][k.second|(1<<j-1)] = 1; 39 } 40 } 41 } 42 for(int j=1;j<=n;j++){ 43 if(i == j) continue; 44 ans = max(ans,min(dp[j][(1<<n)-1],g[i][j])); 45 } 46 } 47 printf("%d\n",ans); 48 } 49 50 void read(){ 51 scanf("%d%d",&n,&m); 52 for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); 53 } 54 55 int main(){ 56 read(); 57 if(n == 1){ 58 int ans = 1e9; 59 for(int i=2;i<=m;i++){ans = min(ans,abs(a[1][i]-a[1][i-1]));} 60 printf("%d\n",ans); 61 return 0; 62 } 63 init(); 64 divide(); 65 return 0; 66 }