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,这两个工具虽然堪称经典.但其单线程的速度越来越不能大软件的下载.于是 ...
随机推荐
- ssm框架集成Quartz定时器
第一步:添加依赖 <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>qu ...
- Linux chmod命令
chmod用于管理文件或目录的权限,文件或目录权限的控制分别以读取(r).写入(w).执行(x)3种 可读可写可执行,抽象的用二进制来表示 1 代表拥有该权限,0 代表没有该权限,这样我们就可以看到 ...
- 解决scrollView中嵌套编辑框导致不能上下滑动的问题
EditText设置maxLines之后,文本行数超过maxLines,会网上折叠,上下滑动能够浏览全部文本. 若EditText外层有scrollView.在EditText上下滑动,不会像正常情况 ...
- Spark Streaming实战
1.Storm 和 SparkStreaming区别 Storm 纯实时的流式处理,来一条数据就立即进行处理 SparkStreaming 微批处理,每次处理 ...
- Tomcat启动慢但是不报错的解决办法
参考文章:https://blog.csdn.net/xiaoxinyu316/article/details/39064003 可以查看下tomat的启动日志,看看有哪些比较耗时的操作: grep ...
- 简单认识TCP/IP协议
HTTP协议—— 简单认识TCP/IP协议 本文转自: https://www.cnblogs.com/roverliang/p/5176456.html 大学没读计算机专业,所以很多的专业知识都 ...
- 禅道ZenTao在windows和Lniux下集成安装环境和一键安装方法整理
一共4种安装方法看官可以根据你自己的实际环境来选择一个都很简单 windows下用禅道官网的一键安装包方法(推荐): 为了简化大家在windows下面的安装,我们在xampp基础上做了禅道的windo ...
- labview 的连接
https://www.youtube.com/watch?v=AsQ56CmnfEA&list=PLp02wZHiCj4tcot7tPumcOeVR51oqzf6V
- 关于websocket通讯
var ws = { init:function(callback){ var _this = this; _this.callback = callback; }, websocket:functi ...
- python字符串搜索
python字符串字串查找 find和index方法 更多0 python 字符串 python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 ...