题目链接: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. python3练习100题——054

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

  2. z3学习档案

    Reference: 看雪-z3巧解逆向 知乎:Z3一把梭 z3 solver学习 使用z3约束求解器解决CTF中的题目 Playing with Z3,hacking the serial chec ...

  3. C#依赖注入 简体demo

      class Program { static void Main(string[] args) { Dal dal = new MySql(); dal.Add(); Dal dal1 = new ...

  4. java开发就业招聘管理系统 ssh源码

    开发环境:    Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+MySql数据库 此项目分为 用户 企业  管理员三种角色 运行效果图

  5. Hadoop学习之路(5)Mapreduce程序完成wordcount

    程序使用的测试文本数据: Dear River Dear River Bear Spark Car Dear Car Bear Car Dear Car River Car Spark Spark D ...

  6. 曼孚科技:AI机器学习领域常用的15个术语

    机器学习是人工智能(AI)的核心,是使计算机具有智能的根本途径.​ 本文整理了一下机器学习领域常用的15个术语,希望可以帮助大家更好的理解这门涉及概率论.统计学.逼近论.凸分析.算法复杂度理论等多个领 ...

  7. 优化公式排版和Beamer相关知识

    做优化的同学可能会碰到排列形如 max    ******* s.t.   ***** = *        ***** > ***        ...    的格式 既要要求 max 和 s ...

  8. 记录 Docker 的学习过程 (日志篇)

    日志收集 elk 在node3上操作 docker pull sebp/elk:5610 node3# sysctl vm.max_map_count=262144 node3# docker run ...

  9. Spring Boot的Web配置

    一.使用YML文件配置多套环境    

  10. ASPxGridView 排序、分页、加载数据必需的三个函数

    protected void ASPxGridViewPoint_OnCustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs ...