牛客练习赛41 D最小相似度
- 最小化相似度最大值==最大化差异度最小值
- 然后bfs跑就没了
以前看过这种蜜汁最短路思路,赛中没想到GG。
#include<algorithm>
#include<vector>
#include<iostream>
#include<math.h>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#include<queue>
#include<assert.h>
#include <iomanip>
#define qcin; ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define clr(x) memset(x,0,sizeof x)
#define fmax(x) memset(x,0x3f,sizeof x)
#define finit(x) memset(x,-1,sizeof x)
#define dis(l,r) r-l+1
#define gstr(str) scanf("%s",str)
#define glen(str) strlen(str)
using namespace std;
typedef long long ll;
typedef pair<ll,ll>pll;
const int maxn = (1<<22);
const int mod = 1e8+7;
const ll inf = 1e18;
typedef int arr[maxn];
typedef char str[maxn];
void file(int x){if(x&&fopen("123.in","r")){freopen("123.in","r",stdin);}}
const long double pi = acos(-1);
int sz,n,m,x;
arr d,vis;
queue<int>Q;
str s;
int main(){
file(1);
scanf("%d%d",&n,&m);
fill(d,d+maxn-1,m);
for(int i=0;i<n;i++){
gstr(s);
int tmp=0;
for(int j=0;j<m;j++){
if(s[j]=='1')tmp|=(1<<j);
}
if(d[tmp]){
d[tmp]=0;
Q.push(tmp);
}
}
while(Q.size()){
int u=Q.front();
Q.pop();
for(int i=0;i<m;i++){
int v=u^(1<<i);
if(d[v]==m){
d[v]=d[u]+1;
Q.push(v);
}
}
}
int ans=0;
for(int i=0;i<(1<<m);i++){
ans=max(ans,d[i]);
}
printf("%d\n",m-ans);
return 0;
}