floyd求最小环

在Floyd的同时,顺便算出最小环。
Floyd算法
 for(k=;k<=n;k++)
{ for(i=;i<k;i++)
for(j=i+;j<k;j++)
if(d[i][j]+m[i][k]+m[k][j]<min)
min=d[i][j]+m[i][k]+m[k][j];
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(d[i][k]+d[k][j]<d[i][j])
d[i][j]=d[i][k]+d[k][j];
}
保证了最外层循环到 k 时所有顶点间已求得以 0...k-1 为中间点的最短路径。一
个环至少有 3 个顶点,设某环编号最大的顶点为 L ,在环中直接与之相连的两个顶点编号
分别为 M 和 N (M,N < L),则最大编号为 L 的最小环长度即为 Graph(M,L) + Graph(N,L) +
Dist(M,N) ,其中 Dist(M,N) 表示以 0...L-1 号顶点为中间点时的最短路径,刚好符合 Floyd
算法最外层循环到 k=L 时的情况,则此时对 M 和 N 循环所有编号小于 L 的顶点组合即
可找到最大编号为 L 的最小环。再经过最外层 k 的循环,即可找到整个图的最小环。
上面是对无向图的情况
若是有向图,只需稍作改动。注意考虑有向图中 2 顶点即可组成环的情况
 
题目:
D. Shortest Cycle
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii, jj (i≠ji≠j) are connected if and only if, aiaiAND aj≠0aj≠0, where AND denotes the bitwise AND operation.

Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.

Input

The first line contains one integer nn (1≤n≤105)(1≤n≤105) — number of numbers.

The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤10180≤ai≤1018).

Output

If the graph doesn't have any cycles, output −1−1. Else output the length of the shortest cycle.

Examples
input

Copy
4
3 6 28 9
output

Copy
4
input

Copy
5
5 12 9 16 48
output

Copy
3
input

Copy
4
1 2 4 8
output

Copy
-1
Note

In the first example, the shortest cycle is (9,3,6,28)(9,3,6,28).

In the second example, the shortest cycle is (5,12,9)(5,12,9).

The graph has no cycles in the third example.

分析:对于大于2 * 64个正数的情况直接输出3;其余的情况怼入vector跑floyd求最小环。

代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + ;
vector<ll> vec;
ll g[][];
ll dis[][];
int num; int floyd()
{
ll res = 1e9;
for (int i = ; i <= num; i++)
for (int j = ; j <= num; j++)
dis[i][j] = g[i][j];
for (int k = ; k <= num; k++)
{
for (int i = ; i < k; i++)
for (int j = i + ; j < k; j++)
res = min(res, dis[i][j] + g[i][k] + g[k][j]);
for (int i = ; i <= num; i++)
for (int j = ; j <= num; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
return res != 1e9 ? res : -;
} int main()
{
int n; cin >> n;
int cnt = ;
ll x; for (int i = ; i <= n; i++)
{
scanf("%lld", &x);
if (x > ) cnt++, vec.push_back(x);
}
if (cnt >= )
{
cout << "" << endl;
return ;
}
num = vec.size();
for (int i = ; i < num; i++)
for (int j = i + ; j < num; j++)
g[i + ][j + ] = g[j + ][i + ] = (vec[i] & vec[j]) ? : inf;
cout << floyd() << endl;
}

最小环-Floyd的更多相关文章

  1. timus1004 最小环()Floyd 算法

    通过别人的数据搞了好久才成功,果然还是不够成熟 做题目还是算法不能融会贯通 大意即找出图中至少3个顶点的环,且将环中点按顺序输出 用floyd算法求最小环 因为floyd算法求最短路径是通过中间量k的 ...

  2. 图的连通性问题之连通和最小环——Floyd算法

    Floyd 判断连通性 d[i][j]仅表示i,j之间是否联通 ;k<=n;k++) ;i<=n;i++) ;j<=n;j++) dis[i][j]=dis[i][j]||(dis[ ...

  3. 最小环(floyd以及dijkstra实现+例题)

    最小环定义 最小环是指在一个图中,有n个节点构成的边权和最小的环(n>=3). 一般来说,最小环分为有向图最小环和无向图最小环. 最小环算法: 直接暴力: 设\(u\)和\(v\)之间有一条边长 ...

  4. 图论--最小环--Floyd模板

    #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...

  5. floyd求最小环 模板

    http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...

  6. hdu 1599 find the mincost route (最小环与floyd算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  7. NOIP 2015提高组复赛

    神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第 ...

  8. POJ1734 - Sightseeing trip

    DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...

  9. poj图论解题报告索引

    最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...

随机推荐

  1. [转载] 管Q某犇借的手写堆

    跟gxy大神还有yzh大神学了学手写的堆,应该比stl的优先队列快很多. 其实就是维护了一个二叉堆,写进结构体里,就没啥了... 据说达哥去年NOIP靠这个暴力多骗了分 合并果子... templat ...

  2. 聊聊C语言的预编译指令include

    "include"相信大家不会陌生,在我们写代码时,开头总会来一句"include XXX".include是干嘛用的,很多教材都提到了,因此这里不会再详细解释 ...

  3. 第九章 webase 分布式中间件平台快速部署

    鉴于笔者以前各大博客教程都有很多人提问,早期建立一个技术交流群,里面技术体系可能比较杂,想了解相关区块链开发,技术提问,请加QQ群:538327407 参考资料:https://webasedoc.r ...

  4. PipelineDB Install and Test

    Installation Prerequisites: CentOS Linux release 7.2.1511 (Core) Download [root@citus1 ~]# wget http ...

  5. py+selenium 明明定位不到元素,但却不报错或是报错AttributeError: 'list' object has no attribute 'click'【已解决】

    问题:定位不到元素,但却不报错或者出现报错AttributeError: 'list' object has no attribute 'click' 如图  或者  解决方法:   将”driver ...

  6. C#3.0新增功能03 隐式类型本地变量

    连载目录    [已更新最新开发文章,点击查看详细] 从 Visual C# 3.0 开始,在方法范围内声明的变量可以具有隐式“类型”var. 隐式类型本地变量为强类型,就像用户已经自行声明该类型,但 ...

  7. MongoDB 启动时关于 NUMA 警告 的分析----(To avoid performance problems)

    1. 需求描述 观察MongoDB的启动Log,会看到一个关于  NUMA 的警告 和 优化建议 --17T17:: I CONTROL [initandlisten] ** WARNING: You ...

  8. 《VR入门系列教程》之17---发布第一个应用

    发布运行     Unity可以支持多种目标平台的发布,包括:桌面端.Web端.移动端.游戏主机端.     发布运行之前的Cubes场景至桌面端,我们先选择File->Build Settin ...

  9. jsp数据交互(一).3

    引入的文件如果是jsp则应定义为***.jspf文件,如果其他文件可定义为***.inc文件,即include file. jsp:include是既可以静态包含又可以动态包含,用jsp:includ ...

  10. internet连接共享被启用时 出现了一个错误 (null)

    服务Firewall和Internet Sharing Service都已启动还是出现此问题 注意到点选允许共享时的提示:当前已为wireless_95364开启...... 随便选一个别的适配器,开 ...