#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;
}

Codeforces Round #580 (Div. 2)D(思维,Floyd暴力最小环)的更多相关文章

  1. Codeforces Round #580 (Div. 1)

    Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...

  2. Codeforces Round #580 (Div. 1) A-E

    Contest Page A Tag:构造 将$a_i$看做一个无穷数列,$i > 2n$时$a_i = a_{i - 2n}$.设$sgn_i = \sum\limits_{j=i+1}^{i ...

  3. Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)

    You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...

  4. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  5. Codeforces Round #143 (Div. 2) (ABCD 思维场)

    题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...

  6. Codeforces Round #395 (Div. 2)(A.思维,B,水)

    A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...

  7. Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)

    A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  8. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  9. Codeforces Round #539 (Div. 2) D 思维

    https://codeforces.com/contest/1113/problem/D 题意 将一个回文串切成一段一段,重新拼接,组成一个新的回文串,问最少切几刀 题解 首先无论奇偶串,最多只会切 ...

随机推荐

  1. LoadRunner通过验证参数判断事物的成功与失败

    if(atoi(lr_eval_string("{Param_DiscountID}")) > 0){ //lr_message("多机多酒:%s",lr ...

  2. json_encode转化索引数组之后依然还是数组的问题

    小坑问题:直接上图 解决方法:(json_encode加入第二个参数) JSON_FORCE_OBJECT

  3. Plastic Bottle Manufacturer Tips: Use Caution For Plastic Bottles

    Plastic bottles use polyester (PET), polyethylene (PE), polypropylene (PP) as raw materials, after a ...

  4. lca最近公共祖先与树上倍增。

    https://vjudge.net/contest/295298#problem/A lca 的题目 求任意两点的距离. A题是在线算法,用st表rmq来实现. https://blog.csdn. ...

  5. opencv:图形绘制与填充

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  6. Abaqus-GUI开发-RSG

    目录 1. GUI开发简介 2. 目标和消息 2.1消息类型和消息ID 2.2消息映射 3. 控件创建 1. GUI开发简介 Abaqus GUI程序开发时,可以采用两种方式创建GUI图形界面. (1 ...

  7. Python学习(四)—— 列表和元组的类中方法

    列表 list:用中括号括起来,用“,”分割每个元素,列表中的元素可以是 数字.字符串.列表.布尔值......所有东西,可以说就是一个“集合” li = [1,3,5,'alex','age',[' ...

  8. IDEA部署项目,并结合Shell脚本运行Java程序

    一.概述 在实际开发中,我们写好的代码,往往打成war包或jar包,通过winscp或其他软件将其上传至服务器,然而这样非常大的一个弊端就是不利于开发,为什么这么说呢?假如我们刚刚将springboo ...

  9. P1567

    最大子数组和问题,dp或者分治.. #include <bits/stdc++.h> #define rep(i, a, b) for(int i = a; i <= b; i++) ...

  10. 纯CSS实现吸顶效果

    position的属性有哪些? {  position: static;  position: relative;  position: absolute;  position: fixed; pos ...