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

最新文章

  1. RecyclerView如何消除底部的分割线
  2. 使用git的分支功能实现定制功能摘取与组合的想法
  3. 在Ubuntu下安装imx6linux系统的交叉编译环境遇到的问题总结
  4. socket编程里的read和recv函数【转载】
  5. bin
  6. Redmine管理项目3-调整用户显示格式
  7. 五分钟秒懂Java日志组件
  8. linux 巨页使用测试以及勘误1
  9. epoll通俗讲解
  10. python算法&amp;二分查找法
  11. Eclipse拷贝动态的web工程修改context root的值
  12. Workspace in use or cannot be created, choose a different one.错误的解决办法
  13. VC 预定义宏
  14. UEditor在asp.netMVC4中的使用,包括上传功能,粘贴表格不显示边框问题
  15. Linux 下 mysql的基本配置
  16. 开机或联网时自启动gunicorn
  17. 【LG4248】[AHOI2013]差异
  18. GMM实战
  19. 分享:Windows2008重启后提示系统恢复选项的解决办法
  20. 20155308 实验四 Android开发基础

热门文章

  1. c++小游戏——2048
  2. [USACO07FEB]银牛派对Silver Cow Party
  3. RabbitMQ实战(三)-高级特性
  4. Vmware centos7无法联网的问题解决
  5. HTML--CSS样式表--格式与布局
  6. Html5web全栈前端开发_angular框架
  7. Django的性能优化
  8. 加深对C#数据类型的认识
  9. Linux网站及工具网站
  10. Java核心技术(卷一)读书笔记——第二章(JAVA/JDK环境配置)