B. Shortest Cycle 无向图求最小环

题意:
给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成)
题解:
逻辑运算 & 是二进制下的运算,题目给的每个权值 a[i] 的范围最大是1018,即二进制下最多64位。
如果64位中有某一位的1的出现数大于 2 了,那么很明显,最小环就是3(该位循环)。
换个说法,在最坏的情况下,给出了 n 个数,其中有超过 128 个不为 0 的数,那么答案一定是3(因为当有128个不为0的数时,64位每一位的个数都是2,只要再随便来个不为0的数,都会出现大于2的位数,构成 3 的环)
所以我们只要考虑不为 0 的数的个数小于130的情况就行了,这个时候 n 就很小了,可以用dfs搜索或者用Flyod求最小环。
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#define ll long long
#define mx 0x3f3f3f3f
using namespace std;
ll way[][],dis[][],num[];
ll n,m,ans; void init()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i!=j&&num[i]&num[j])
way[i][j]=dis[i][j]=;
else
way[i][j]=dis[i][j]=mx;
}
}
}
void floyd()
{
ans=mx;
for(int k=;k<=n;k++)
{
for(int i=;i<k;i++)
{
for(int j=i+;j<k;j++)
ans=min(ans,dis[i][j]+way[i][k]+way[k][j]);//接成环
} for(int i=;i<=n;i++)//求最短路
for(int j=;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
int main()
{
while(~scanf("%lld",&n))
{
ll cnt=,x;
for(int i=;i<=n;i++)
{
scanf("%lld",&x);
if(x)
num[cnt++]=x;
}
n=cnt;
if(n>)
printf("3\n");
else
{
init();
floyd();
if(ans==mx)
printf("-1\n");
else
printf("%lld\n",ans);
}
}
return ;
}
B. Shortest Cycle 无向图求最小环的更多相关文章
- CF 1206D - Shortest Cycle Floyd求最小环
Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...
- poj1734Sightseeing trip——无向图求最小环
题目:http://poj.org/problem?id=1734 无向图求最小环,用floyd: 在每个k点更新f[i][j]之前,以k点作为直接连到i,j组成一个环的点,这样找一下最小环: 注意必 ...
- D. Shortest Cycle(floyd最小环)
D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- [CF580C]Shortest Cycle(图论,最小环)
Description: 给 \(n\) 个点的图,点有点权 \(a_i\) ,两点之间有边当且仅当 \(a_i\ \text{and}\ a_j \not= 0\),边权为1,求最小环. Solut ...
- POJ1734无向图求最小环
题目:http://poj.org/problem?id=1734 方法有点像floyd.若与k直接相连的 i 和 j 在不经过k的情况下已经连通,则有环. 注意区分直接连接和间接连接. * 路径记录 ...
- [Codeforces 1205B]Shortest Cycle(最小环)
[Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...
- Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)
You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...
- D. Shortest Cycle
D. Shortest Cycle A[i]&A[j]!=0连边, 求图中最小环 N>128 时必有3环 其他暴力跑 folyd最小环 #include<bits/stdc++.h ...
- FLOYD 求最小环
首先 先介绍一下 FLOYD算法的基本思想 设d[i,j,k]是在只允许经过结点1…k的情况下i到j的最短路长度则它有两种情况(想一想,为什么):最短路经过点k,d[i,j,k]=d[i,k,k- ...
随机推荐
- RemoteView设置高度
刚开始内层LinearLayout直接用 android:layout_height="match_parent" <?xml version="1.0" ...
- Linux - 命令 - top命令
负载检查:https://blog.csdn.net/HANLIPENGHANLIPENG/article/details/79172053 参考:https://blog.csdn.net/gxia ...
- Bugku-CTF加密篇之easy_crypto(0010 0100 01 110 1111011 11 11111 010 000 0 001101 1010 111 100 0 001101 01111 000 001101 00 10 1 0 010 0 000 1 01111 10 11110 101011 1111101)
easy_crypto 0010 0100 01 110 1111011 11 11111 010 000 0 001101 1010 111 100 0 001101 01111 000 00110 ...
- POJ-1087 A Plug for UNIX (网络流)
思路 电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no rece ...
- HDU4280 Island Transport
ISAP求最大流模板 #include<cstdio> #include<cstring> #include<algorithm> #include<iost ...
- ZOJ1008 Gnome Tetravex
DFS+剪枝~ #include<bits/stdc++.h> using namespace std; ][]; int N; int cnt; ]; ]; unordered_map& ...
- Hibernate学习(四)
一对多测试案例 1.类图 2.创建表 drop table if exists t_student ; drop table if exists t_class ; create table t_cl ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 高级索引
import numpy as np x = np.array([[1, 2], [3, 4], [5, 6]]) y = x[[0,1,2], [0,1,0]] print (y) import n ...
- Java学习资源 - J2SE
java.lang包教程 Java集合类详解 Java回顾之集合 Java回顾之序列化 Java回顾之反射 深入理解Java:类加载机制及反射 Java 下高效的反射工具包 ReflectASM 使用 ...
- BFS迷宫问题
链接:https://ac.nowcoder.com/acm/challenge/terminal来源:牛客网 小明现在在玩一个游戏,游戏来到了教学关卡,迷宫是一个N*M的矩阵. 小明的起点在地图中用 ...