codeforces781D Axel and Marston in Bitland
题目链接:codeforces781D
正解:$bitset$+状压$DP$
解题报告:
考虑用$f[t][0、1][i][j]$表示从$i$出发走了$2^t$步之后走到了$j$,且第一步是走的$0$或者$1$,这个状态是否存在。
转移式子的话就是$f[t][z][i][j]$$|=$$f[t-1][z][i][k]$ & $f[t-1][z$ ^ $1][k][j]$。
但是复杂度太高了,我们考虑压位,因为反正每个状态都只记录了$0$、$1$,那么我还不如把最后那一维(也就是上面的$j$)那一维变成一个二进制数,第$j$位表示取$j$的时候是否为$1$。
$N$只有$500$,开一个$500$的$bitset$就可以了。
总复杂度:$O(\frac{n^3*log(10^{18})}{60})$。
ps:因为$bitset$是$60$位一压所以复杂度$/60$,卡常神器!
//It is made by ljh2000
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 520;
const LL inf = (LL)(1e18);
int n,m;
bitset<520>f[63][2][MAXN],tmp,g;
LL ans; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void work(){
n=getint(); m=getint(); int x,y,z;
for(int i=1;i<=m;i++) {
x=getint(); y=getint(); z=getint();
f[0][z][x][y]=1;
}
for(int s=1;s<=60;s++) {
for(int i=1;i<=n;i++)
for(int k=1;k<=n;k++) {
//搞清楚第一步做出的选择!
if(f[s-1][0][i][k])
f[s][0][i]|=f[s-1][1][k]; if(f[s-1][1][i][k])
f[s][1][i]|=f[s-1][0][k];
}
}
for(int i=1;i<=n;i++)
if(f[60][0][1][i]){
printf("-1");
return ;
} g[1]=1; int tag=0;
for(int s=59;s>=0;s--) {//贪心地选
tmp=0;//清空!!!
for(int i=1;i<=n;i++)
if(g[i]) tmp|=f[s][tag][i];
if(tmp.count()) g=tmp,ans+=(1LL<<s),tag^=1;
}
if(ans>inf) printf("-1");
else printf("%I64d",ans);
} int main()
{
work();
return 0;
}
//有志者,事竟成,破釜沉舟,百二秦关终属楚;苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
codeforces781D Axel and Marston in Bitland的更多相关文章
- CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
Axel and Marston in Bitland 好开心第一次补$F$题虽然是$Div.2$ 题意: 一个有向图,每条边是$0$或$1$,要求按如下规则构造一个序列然后走: 第一个是$0$,每次 ...
- Codeforces 781D Axel and Marston in Bitland 矩阵 bitset
原文链接https://www.cnblogs.com/zhouzhendong/p/CF781D.html 题目传送门 - CF781D 题意 有一个 n 个点的图,有 m 条有向边,边有两种类型: ...
- Codeforces 781D Axel and Marston in Bitland
题目链接:http://codeforces.com/contest/781/problem/D ${F[i][j][k][0,1]}$表示是否存在从${i-->j}$的路径走了${2^{k}} ...
- Axel and Marston in Bitland CodeForces - 782F (bitset优化)
题目链接 $dp[0/1][i][x][y]$表示起始边为0/1, 走$2^i$ 步, 是否能从$x$走到$y$ 则有转移方程 $dp[z][i][x][y]\mid=dp[z][i-1][x][k] ...
- CodeForces 781D Axel and Marston in Bitland DP
题意: 有一个\(n\)个点\(m\)条边的无向图,边有两种类型,分别用\(0\)和\(1\)标识 因此图中的任意一条路径都对应一个\(01\)字符串 定义一个无限长的字符串\(s\): 开始令\(s ...
- Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)
Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...
- Codeforces Round#403 (Div. 1)
唉,昨天晚上迷迷糊糊地去打cf,结果fst两题,掉回蓝了... A.Andryusha and Colored Balloons 题意:给定一棵树,任意两个距离小等于二的点不能染相同的颜色,求最小颜色 ...
- CodeForces Round #403 (Div.2) A-F
精神不佳,选择了在场外同步划水 没想到实际做起来手感还好,早知道就报名了…… 该打 未完待续233 A. Andryusha and Socks 模拟,模拟大法好.注意每次是先判断完能不能收进柜子,再 ...
- Axel替代wget
Axel替代wget 2011年11月10日admin发表评论阅读评论 Linux下用的最多的下载工具莫过于wget和curl,这两个工具虽然堪称经典.但其单线程的速度越来越不能大软件的下载.于是 ...
随机推荐
- 爬虫之Xpath详解
XPath介绍 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 ...
- 《iOS Human Interface Guidelines》——Popover
弹出框 弹出框是当人们点击一个控件或屏幕上一个区域时显示的一个暂时的界面. API NOTE 在iOS 8及以后的系统中.你能够使用UIPopoverPresentationController来显示 ...
- 大量高清文字版PDF编程书籍下载
下载地址 :http://zhaojucai.com/download.html 文件夹: 数学之美(第二版) Java编程思想(第4版) 图灵程序设计丛书:Python基础教程(第2版)(修订版) ...
- Docker中安装配置Oracle数据库
本文使用的OS是Ubuntu([16.04.1_server][1])[注:Ubuntu是安装在vmware虚拟机上的]. 其他的Oracle连接工具:[sqldeveloper-4.1.5.21.7 ...
- python学习笔记(十二)python操作redis
1.python要操作redis 首先需要安装redis模块,然后导入才能使用 安装:pip install redis 导入:import redis 2.连接redis r = redis.Red ...
- django生产环境部署
测试环境:linux centos7下 1.安装uwsgi python3下安装: pip3 install uwsgi python2下安装: pip install uwsgi 如果是系统自带的p ...
- Uboot mmc命令解析&NAND flash uboot命令详解
转载:http://blog.csdn.net/simonjay2007/article/details/43198353 一:mmc的命令如下: 1:对mmc读操作 mmc read addr bl ...
- PAT 1093 Count PAT's[比较]
1093 Count PAT's (25 分) The string APPAPT contains two PAT's as substrings. The first one is formed ...
- boost enable_shared_from_this
关于shared_ptr和weak_ptr看以前的:http://www.cnblogs.com/youxin/p/4275289.html The header <boost/enable_s ...
- (C++) Assertion failed: !"Bad error code", file VMem.c, line 715
(C++) Assertion failed: !"Bad error code", file VMem.c, line 715 Misc error. myInterface F ...