Shortest Cycle

题意

有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边。问图中的最小环。

思路

可以发现当非0数的个数很大,比如大于200时,一定存在长度为3的环。

如果小于200, 我们就用到了Floyd求最小环的技巧。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
#include <unordered_map>
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a) 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll; const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int mod = ; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
/**********showtime************/
const int maxn = 2e5+;
ll a[maxn];
int cnt[];
ll ans;
ll dp[][], g[][];
vector<ll>b;
int main(){
int n;
scanf("%d", &n);
int cnt = ;
for(int i=; i<=n; i++){
scanf("%lld", &a[i]);
if(a[i] == ) cnt++;
else b.pb(a[i]);
}
if(n - cnt > ) puts("");
else {
int n = b.size();
for(int i=; i<=n; i++){
for(int j=; j<=n; j++) {
if(i == j) dp[i][j] = ,g[i][j] = ;
else dp[i][j] = dp[j][i] = inf, g[i][j] = inf;
}
}
for(int i=; i<n; i++) {
for(int j=i+; j<n; j++) {
if((b[i] & b[j]) > ) {
dp[i+][j+] = ;
dp[j+][i+] = ;
g[i+][j+] = ;
g[j+][i+] = ;
}
}
}
ans = inf;
for(int k=; k<=n; k++) {
for(int i=; i<k; i++) {
for(int j=i+; j<k; j++) {
ans = min(ans, dp[i][j] + g[k][i] + g[k][j]);
}
}
for(int i=; i<=n; i++) {
for(int j=; j<=n; j++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
}
}
if(ans == inf) puts("-1");
else printf("%lld\n", ans);
}
return ;
}

CF 1206D - Shortest Cycle Floyd求最小环的更多相关文章

  1. B. Shortest Cycle 无向图求最小环

    题意: 给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成) 题解:逻辑运算 & 是二进制 ...

  2. 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  3. Floyd求最小环!(转载,非原创) 附加习题(原创。)HDU-1599

    //Floyd 的 改进写法可以解决最小环问题,时间复杂度依然是 O(n^3),储存结构也是邻接矩阵 int mincircle = infinity; Dist = Graph; ;k<nVe ...

  4. 2018.09.15 hdu1599find the mincost route(floyd求最小环)

    传送门 floyd求最小环的板子题目. 就是枚举两个相邻的点求最小环就行了. 代码: #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f ...

  5. 【BZOJ 1027】 (凸包+floyd求最小环)

    [题意] 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金 ...

  6. 算法复习——floyd求最小环(poj1734)

    题目: 题目描述 N 个景区,任意两个景区之间有一条或多条双向的路来连接,现在 Mr.Zeng 想找一条旅游路线,这个路线从A点出发并且最后回到 A 点,假设经过的路线为 V1,V2,....VK,V ...

  7. floyd求最小环 模板

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

  8. 弗洛伊德Floyd求最小环

    模板: #include<bits/stdc++.h> using namespace std; ; const int INF = 0xffffff0; ]; void Solve(in ...

  9. POJ1734 Sightseeing trip (Floyd求最小环)

    学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...

随机推荐

  1. android 界面提示框架WisdomProgressHUD,为金典而生

    一:简述 今天给android开发者们,推荐一个金典的界面提示框架WisdomProgressHUD,使用简洁方便. WisdomProgressHUD 是一个半透明的 HUD 指示器. Wisdom ...

  2. c#小灶——初识c#

    提到c#,就不得不说.net,.net是微软开发的一个平台,简单来说,在这个平台上,可以编写.运行程序.可能很多人觉得这个平台离我们很遥远,其实不然,这个平台就一直在我们的windows操作系统里,默 ...

  3. 自定义SWT控件三之搜索功能下拉框

    3.搜索功能下拉弹出框 package com.view.control.select; import java.util.ArrayList; import java.util.LinkedList ...

  4. npm包开发与发布

    把通用的功能开发成npm包,便用使用和维护,更重要的是可以分享给广大的开发者,是不是很激动人心! 那么,步骤如下: 1.创建项目 创建项目目录,npm init ,根据需要输入配置信息(建完后也可以在 ...

  5. 全屏滚动插件pagePiling.js

    全屏滚动效果是最近非常流行的网页设计形式,带给用户良好的视觉和交互体验.pagePiling.js 这款 jQuery 插件可以帮助前端开发人员轻松实现这种效果.支持所有的主流浏览器,包括IE8+,支 ...

  6. hadoop的基础思想

    转载 http://www.superwu.cn/2014/01/10/963 1.1.1. hadoop的核心思想 Hadoop包括两大核心,分布式存储系统和分布式计算系统.1.1.1.1. 分布式 ...

  7. Postman系列二:Postman中get接口实战讲解(接口测试介绍,接口测试流程,头域操作)

    一:接口测试介绍 接口测试:就是针对软件对外提供服务的接口输入输出进行测试,以及接口间相互逻辑的测试,验证接口功能和接口描述文档的一致性. 接口测试好处:接口测试通常能对系统测试的更为彻底,更高的保障 ...

  8. Linux--shel的if判断语句--05

    if条件语句的使用格式: 1.单分支语句 if [ 条件 ];then 执行语句 fi 注意:[ 条件 ] :条件与中括号要用空格分割:下面的语句同理. 2.双分支语句 if [ 条件 ];then ...

  9. SQL中一些实用的快捷键

    Ctrl+A全选 快速选中一行: 若光标在这条语句末尾用Shift+Home 若光标在这条语句开头用Shift+End Ctrl+K+U快捷注释本行 Ctrl+K+C反注释 Ctrl+R  关闭下面的 ...

  10. Spring Cloud版本

    Spring Cloud版本 Spring Cloud版本演进情况如下: 版本名称 版本 Finchley snapshot版 Edgware snapshot版 Dalston SR1 当前最新稳定 ...