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;
}
Codeforces Round #580 (Div. 2)D(思维,Floyd暴力最小环)的更多相关文章
- Codeforces Round #580 (Div. 1)
Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...
- 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 ...
- 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 ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #143 (Div. 2) (ABCD 思维场)
题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...
- 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 ...
- 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 ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces Round #539 (Div. 2) D 思维
https://codeforces.com/contest/1113/problem/D 题意 将一个回文串切成一段一段,重新拼接,组成一个新的回文串,问最少切几刀 题解 首先无论奇偶串,最多只会切 ...
随机推荐
- Centos6.10-FastDFS-Tracker.conf示例配置
Centos610系列配置 FastDFS_Tracker.conf示例配置 # is this config file disabled # false for enabled # true for ...
- c# /MVC设置类的自定义特性
public class MarkStaticAttribute:Attribute { public MarkStaticAttribute(bool mark=true) { _IsMark = ...
- Linq Group by获取数量和数据
主表: public partial class Activity { [Key] public int pkActivity { get; set; } public int fkEmployee ...
- Dockerfile文档编写
图片显示问题,附上有道云笔记中链接:http://note.youdao.com/noteshare?id=fba6d2f53fd6447ba32c3b7accfeb89b&sub=B36B5 ...
- Git的安装和使用(转载)
转载博客连接:https://www.cnblogs.com/qijunjun/p/7137207.html 实际项目开发中,我们经常会用一些版本控制器来托管自己的代码,今天就来总结下Git的相关用法 ...
- 【代码学习】PYTHON 生成器
一.生成器 一遍循环一遍计算的机制,称为生成器 二.生成器的特点: 1.节约内存 2.迭代到下一次的调用时,所使用的参数都是第一次所保留下的,即是说,在整个所有函数调用的参数都是第一次所调用时保留的, ...
- 16,div+css的布局较table布局有什么优点?
改版的时候更加方便,只要改css文件 页面加载速度更快,结构化清晰,页面显示简洁 表现与结构相分离 易于搜索引擎优化,排名更靠前
- Django ORM 常用的13个方法
介绍一个可以以py脚本方式运行ORM操作的方法: 可在项目内新建个py文件,复制项目内manage.py文件中的以下代码: if __name__ == "__main__": o ...
- 命令行选项解析函数getopt()
1.定义: int getopt(int argc, char * const argv[], const char *optstring); 2.描述: getopt是用来解析命令行选项参数的,但是 ...
- dom4j+反射实现bean与xml的相互转换
由于目前在工作中一直用的dom4j+反射实现bean与xml的相互转换,记录一下,如果有不正确的地方欢迎大家指正~~~ 一.反射机制 在此工具类中使用到了反射技术,所以提前也看了一些知识点,例如:ht ...