Codeforces Round #580 (Div. 2)D(思维,Floyd暴力最小环)

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
const int maxn=300;
const int inf=1e9;
long long a[100007];
long long b[100007];
int val[maxn + 1][maxn + 1]; // 原图的邻接矩阵
inline int floyd(const int &n) {
static int dis[maxn + 1][maxn + 1]; // 最短路矩阵
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) dis[i][j] = val[i][j]; // 初始化最短路矩阵
long long ans = inf;
for (int k = 1; k <= n; ++k) {
for (int i = 1; i < k; ++i)
for (int j = 1; j < i; ++j)
ans = min(ans, 1ll*dis[i][j] + val[i][k] + val[k][j]); // 更新答案
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); // 正常的 floyd 更新最短路矩阵
}
return ans;
}
int main(){
int n;
cin>>n;
int num=0;
for(int i=1;i<=n;++i){
cin>>a[i];
if(a[i])
b[++num]=a[i];
}
if(num>128)//说明至少有一位上有3个及以上的点,定能连成长度为3的环
cout<<3;
else{
for(int i=1;i<=num;++i){
for(int j=1;j<=num;++j){
if(i==j)
continue;
if(b[i]&b[j])
val[i][j]=1;
else
val[i][j]=1e9;
}
}
int ans=floyd(num);//floyd暴力最小环
if(ans==1e9)
cout<<-1;
else
cout<<ans;
}
return 0;
}

posted @ 2019-08-19 15:17  sewage  阅读(258)  评论(0编辑  收藏  举报