Codeforces1102F Elongated Matrix 【状压DP】

时间:2024-04-20 15:37:27

题目分析:

这题瞎搞一个哈密尔顿路,对于起点不同的分开跑就可以过了。

$O(n^3*2^n)$

 #include<bits/stdc++.h>
using namespace std; const int maxn = ;
const int maxm = ; int n,m;
int a[maxn][maxm],f[maxn][maxn],g[maxn][maxn]; int dp[maxn][(<<)+],arr[maxn][(<<)+]; void init(){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
f[i][j] = g[i][j] = 1e9;
for(int k=;k<=m;k++)f[i][j] = min(f[i][j],abs(a[i][k]-a[j][k]));
for(int k=;k<m;k++) g[i][j]=min(g[i][j],abs(a[i][k+]-a[j][k]));
}
}
} queue<pair<int,int> > q;
void divide(){
int ans = ;
for(int i=;i<=n;i++){
memset(dp,,sizeof(dp));
memset(arr,,sizeof(arr));
dp[i][<<i-] = 1e9; arr[i][<<i-] = ;
q.push(make_pair(i,<<i-));
while(!q.empty()){
pair<int,int> k = q.front(); q.pop();
for(int j=;j<=n;j++){
if(k.second&(<<j-)) continue;
dp[j][k.second|(<<j-)] =
max(dp[j][k.second|(<<j-)],min(dp[k.first][k.second],f[k.first][j]));
if(!arr[j][k.second|(<<j-)]){
q.push(make_pair(j,k.second|(<<j-)));
arr[j][k.second|(<<j-)] = ;
}
}
}
for(int j=;j<=n;j++){
if(i == j) continue;
ans = max(ans,min(dp[j][(<<n)-],g[i][j]));
}
}
printf("%d\n",ans);
} void read(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) for(int j=;j<=m;j++) scanf("%d",&a[i][j]);
} int main(){
read();
if(n == ){
int ans = 1e9;
for(int i=;i<=m;i++){ans = min(ans,abs(a[][i]-a[][i-]));}
printf("%d\n",ans);
return ;
}
init();
divide();
return ;
}