题目链接:http://codeforces.com/contest/1206/problem/D

给n个点,如果点a[ i ] &a[ j ] 不为0,则点a[ i ] 和 a[ j ] 直接可以连接双向边,如果这些点形成的图中有环,求最短路径的环,如果没有输出-1.

思路:整体是用floyd求最短环,但是数据量很大,有1e5的数据,空跑floyd直接超时,但是由于题目的特殊性,两数相与不为0才有边,那么任意的a[ i ]二进制是有63位的,那么一个数字的二进制最多有63个1,如果总体数字的二进制63位中的任意一位存在三个以上的1,说明有三个数相与都是不为0的,三个数可以互相连边,那么最短环一定是3,靠这个结论解答可以大大缩短时间复杂度,如果a[ i ](不为0)个数多到某一个Max值时,则某一位上1的个数一定会超过3,那么这个Max值具体是多少呢?没有详细计算,但是一定不会超过63 * 2 = 126个,因为就算每一位分配2个“1”,第127个必定使得一位有3个“1”,那么可以将Max可以暂定为126,也就是说1e5的计算数据大大减少到了126,这样跑floyd就不会超时了。注意如果a[ i ] = 0,直接可以不会加入cnt计数中,因为0与任意数相与都是0.

AC代码:

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#define maxn 100005
#define inf 0x3f3f3f3f
using namespace std;
long long int a[maxn];
long long int dist[200][200];
long long int g[200][200];
int n;
int cnt = 1;
long long int ans = 0x3f3f3f3f;
void floyd(){
for(long long int k = 1;k<=cnt;k++){
for(long long int i = 1;i<k;i++){
for(long long int j = i+1;j<k;j++){
if(dist[i][j] == inf || g[j][k] == inf || g[i][k] == inf){
continue;
}
ans = min(ans,dist[i][j]+g[j][k]+g[k][i]);
}
} for(long long int i = 1;i<=cnt;i++){
for(long long int j = 1;j<=cnt;j++){
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
int main(){
cin>>n;
for(long long int i = 1;i<=n;i++){
long long int t;
cin>>t;
if(t){
a[cnt] = t;
cnt++;
}
}
if(cnt > 126){
cout<<3;
return 0;
}
for(long long int i = 1;i<=cnt;i++){
for(long long int j = i+1;j<=cnt;j++){
if((a[i] & a[j]) ){
dist[i][j] = 1,dist[j][i] = 1;
g[i][j] = 1,g[j][i] = 1;
}
else{
dist[i][j] = inf,dist[j][i] = inf;
g[i][j] = inf,g[j][i] = inf;
}
}
}
floyd();
if(ans == inf ){
cout<<-1;
return 0;
}
cout<<ans;
return 0;
}

codeforce D. Shortest Cycle(floyd求最短环)的更多相关文章

  1. CF 1206D - Shortest Cycle Floyd求最小环

    Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...

  2. B. Shortest Cycle 无向图求最小环

    题意: 给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成) 题解:逻辑运算 & 是二进制 ...

  3. bzoj 1486: [HNOI2009]最小圈 dfs求负环

    1486: [HNOI2009]最小圈 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1022  Solved: 487[Submit][Status] ...

  4. D. Shortest Cycle(floyd最小环)

    D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  6. [cf557d]Vitaly and Cycle(黑白染色求奇环)

    题目大意:给出一个 n 点 m 边的图,问最少加多少边使其能够存在奇环,加最少边的情况数有多少种. 解题关键:黑白染色求奇环,利用数量分析求解. 奇环:含有奇数个点的环. 二分图不存在奇环.反之亦成立 ...

  7. D. Shortest Cycle

    D. Shortest Cycle A[i]&A[j]!=0连边, 求图中最小环 N>128 时必有3环 其他暴力跑 folyd最小环 #include<bits/stdc++.h ...

  8. USACO 4.1 Fence Loops(Floyd求最小环)

    Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...

  9. Codeforces 1206 D - Shortest Cycle

    D - Shortest Cycle 思路:n大于某个值肯定有个三元环,否则floyd找最小环. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...

随机推荐

  1. 使用javacv 截取视频指定帧节

    个人博客 地址:https://www.wenhaofan.com/article/20190407105818 引入依赖 <dependency> <groupId>org. ...

  2. Python标准库之hashlib模块与hmac模块

    hashlib模块用于加密相关的操作.在Python 3.x里代替了md5模块和sha模块,主要提供 SHA1.SHA224.SHA256.SHA384.SHA512 .MD5 算法.如果包含中文字符 ...

  3. python3练习100题——054

    题目:取一个整数a从右端开始的4〜7位. a=input('please input a num:') print(a[-7:-3])

  4. IDEA科学使用

    今天莫名激活码又用不起了有能力的支持正版吧 ,要用的时候又去网上到处找然后发现各种用不了,去淘宝又怕被骗博主就是过来人 ,总算下定决心写一篇一劳永逸的方法.. 方法一:合理使用激活码 用过idea的都 ...

  5. Cenos7 学习笔记

    一.nmtui nmtui——Text User Interface for controlling NetworkManager,这是一个NetworkManager服务的网卡接口配置工具,能实现在 ...

  6. 如何处理pom文件中没有找到HUB检查到高危漏洞的依赖包

    最近使用HUB工具检查到maven工程中存在高危险漏洞,虽然定位到具体的引用包了,但是在pom文件中却没有发现该依赖包.此时,我们就需要用到这条命令mvn dependency:tree,该命令会将m ...

  7. AOPS论坛上100+100个积分

    100+10 rare and irresistible integrals I bring you many beautiful integrals that I have collected ov ...

  8. Codeforces 540A - Combination Lock

    Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he ...

  9. Qt Gui 第三章~第四章

    一.图片资源的调用 如上图是我图片存放的位置跟qrc的命名: 如下则是qrc对应的编写格式: <RCC> <qresource> <file>images/icon ...

  10. linux - 查看是否安装 apache 以及 apache 版本

    命令 apachectl -v httpd -v 备注:这两个命令的作用一样 结果