CF 1206D - Shortest Cycle Floyd求最小环
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求最小环的更多相关文章
- B. Shortest Cycle 无向图求最小环
题意: 给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成) 题解:逻辑运算 & 是二进制 ...
- 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】
度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...
- Floyd求最小环!(转载,非原创) 附加习题(原创。)HDU-1599
//Floyd 的 改进写法可以解决最小环问题,时间复杂度依然是 O(n^3),储存结构也是邻接矩阵 int mincircle = infinity; Dist = Graph; ;k<nVe ...
- 2018.09.15 hdu1599find the mincost route(floyd求最小环)
传送门 floyd求最小环的板子题目. 就是枚举两个相邻的点求最小环就行了. 代码: #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f ...
- 【BZOJ 1027】 (凸包+floyd求最小环)
[题意] 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金 ...
- 算法复习——floyd求最小环(poj1734)
题目: 题目描述 N 个景区,任意两个景区之间有一条或多条双向的路来连接,现在 Mr.Zeng 想找一条旅游路线,这个路线从A点出发并且最后回到 A 点,假设经过的路线为 V1,V2,....VK,V ...
- floyd求最小环 模板
http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...
- 弗洛伊德Floyd求最小环
模板: #include<bits/stdc++.h> using namespace std; ; const int INF = 0xffffff0; ]; void Solve(in ...
- POJ1734 Sightseeing trip (Floyd求最小环)
学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...
随机推荐
- spring @Required注解
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-required-ann ...
- Linux系统管理----目录与文件管理作业习题
chapter02 - 03 作业 1. 分别用cat \tac\nl三个命令查看文件/etc/ssh/sshd_config文件中的内容,并用自己的话总计出这三个文档操作命令的不同之处? cat ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
- 消息中间件——RabbitMQ(一)Windows/Linux环境搭建(完整版)
前言 最近在学习消息中间件--RabbitMQ,打算把这个学习过程记录下来.此章主要介绍环境搭建.此次主要是单机搭建(条件有限),包括在Windows.Linux环境下的搭建,以及RabbitMQ的监 ...
- Java8中的流操作-基本使用&性能测试
为获得更好的阅读体验,请访问原文:传送门 一.流(Stream)简介 流是 Java8 中 API 的新成员,它允许你以声明式的方式处理数据集合(通过查询语句来表达,而不是临时编写一个实现).这有点儿 ...
- 【vue】------ 路由创建 ------ 【William】
路由常用的配置项: path:路由请求的路径 component:路由匹配成功后需要渲染的组件或者页面 tag:改变组件内部渲染的元素 假设组件内部渲染的是a标签 tag="li" ...
- 关于ajax异步请求的一个细节问题
首先描述一下问题场景:我们正在做一个汽车出租项目,使用maven+ssm+easyui来完成,这个问题是在做汽车办理出租业务的时候出现的. 问题描述:在使用ajax发送异步请求时,遇到一个问题,就是在 ...
- Zookeeper的命令行操作(三)
Zookeeper的命令行操作 1. ZooKeeper服务命令 在准备好相应的配置之后,可以直接通过zkServer.sh 这个脚本进行服务的相关操作 1. 启动ZK服务: sh bin/zkSer ...
- Codeforces Round #574 (Div. 2)——C. Basketball Exercise(简单DP)
题目传送门 题意: 输入n,给出两组均为 n个数字的数组a和b,轮流从a和b数组中取出一个数字,要求严格按照当前所选数字的数组下标比上一个所选数字的数组下标更大,计算能够取出的数字加起来的总和最大能为 ...
- Redhat Linux 系统上安装JDK 1.7
作者:潇湘隐者 出处:http://www.cnblogs.com/kerrycode/ 步骤1:下载JDK 1.7 安装包 JDK 1.7 下载地址:http://www.oracle.com/t ...