最小环-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];
}
个环至少有 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 的循环,即可找到整个图的最小环。
1 second
256 megabytes
standard input
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.
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).
If the graph doesn't have any cycles, output −1−1. Else output the length of the shortest cycle.
4
3 6 28 9
4
5
5 12 9 16 48
3
4
1 2 4 8
-1
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的更多相关文章
- timus1004 最小环()Floyd 算法
通过别人的数据搞了好久才成功,果然还是不够成熟 做题目还是算法不能融会贯通 大意即找出图中至少3个顶点的环,且将环中点按顺序输出 用floyd算法求最小环 因为floyd算法求最短路径是通过中间量k的 ...
- 图的连通性问题之连通和最小环——Floyd算法
Floyd 判断连通性 d[i][j]仅表示i,j之间是否联通 ;k<=n;k++) ;i<=n;i++) ;j<=n;j++) dis[i][j]=dis[i][j]||(dis[ ...
- 最小环(floyd以及dijkstra实现+例题)
最小环定义 最小环是指在一个图中,有n个节点构成的边权和最小的环(n>=3). 一般来说,最小环分为有向图最小环和无向图最小环. 最小环算法: 直接暴力: 设\(u\)和\(v\)之间有一条边长 ...
- 图论--最小环--Floyd模板
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> ...
- floyd求最小环 模板
http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...
- 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 ...
- NOIP 2015提高组复赛
神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第 ...
- POJ1734 - Sightseeing trip
DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
随机推荐
- 深入学习Spring框架(四)- 事务管理
1.什么是事务? 事务(Transaction)是一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位,是数据库环境中的逻辑工作单位.事务是为了保证数据库的完整性.例如:A给B转账,需 ...
- Java中的Lambda表达式简介及应用
在接触Lambda表达式.了解其作用之前,首先来看一下,不用Lambda的时候我们是怎么来做事情的. 我们的需求是,创建一个动物(Animal)的列表,里面有动物的物种名,以及这种动物是否会跳,是否会 ...
- JavaScript剩余操作符Rest Operator
本文适合JavaScript初学者阅读 剩余操作符 之前这篇文章JavaScript展开操作符(Spread operator)介绍讲解过展开操作符.剩余操作符和展开操作符的表示方式一样,都是三个点 ...
- blog更新
特别感谢: yu__xuan (啊这位着重感谢,多次帮助,于是帮他宣传一下https://www.cnblogs.com/poi-bolg-poi/) widerg 为两位大佬撒花~
- stack用法,queue用法,
stack stack 模板类的定义在头文件中. stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要 的,在不指定容器类型时,默认的容器类型为deque. 定义st ...
- 个人永久性免费-Excel催化剂功能第43波-文本处理类函数增强
Excel的函数有400多个,真正常用的50多个,而常有的文本处理类函数也不多,不是因为文本类处理简单,而是Excel真的有点挤牙膏式的每个版本更新那么几个小函数,普通用户等得急切,但实际上这些小函数 ...
- 个人永久性免费-Excel催化剂功能第25波-小白适用的文本处理功能
翻看各大插件,都不约而同地出现系列文本处理的功能,自己在使用Excel过程中,在临时性的需求时,也会用上这几种文本处理,但仅适用于小范围的使用,使用这些功能不是数据处理的正确的之道,数据处理的核心需求 ...
- nginx CRLF(换行回车)注入漏洞复现
nginx CRLF(换行回车)注入漏洞复现 一.漏洞描述 CRLF是”回车+换行”(\r\n)的简称,其十六进制编码分别为0x0d和0x0a.在HTTP协议中,HTTP header与HTTP Bo ...
- linux初学者-用户管理篇
linux的用户管理是非常以后工作中重要的一部分,也是linux系统安全的防线. 1.用户理解 那么到底什么是用户呢?用户就是系统使用者的身份. 用户是以怎样的方式储存在计算机中的呢?在系统中用户存储 ...
- leetcode 48 矩阵旋转可以这么简单
一行代码解决矩阵旋转(方法三). 方法1: 坐标法 def rotate(self, matrix): n = len(matrix) # 求出矩阵长度 m = (n + 1) // 2 # 求出层数 ...