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,这两个工具虽然堪称经典.但其单线程的速度越来越不能大软件的下载.于是 ...
随机推荐
- SVN导出Maven项目
1.从SVN检出项目 -> 作为工作空间中的项目检出 2.转换成Maven project 3.将Maven Project 转化成 Maven app 右击项目-> Properties ...
- 360UI 界面框架 即QUI框架与EXT比较
EXTJS框架是非常全面和成熟的,这是因为它发展的年头久远,并且有全世界的EXTJS爱好者为其出谋献策,它的组件库尤其是DataGrid组件无人能出其右.我在最初也考虑过使用EXTJS来做项目,学习了 ...
- 0604-Zuul构建API Gateway-Zuul的回退
一.概述 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#hystrix-f ...
- G729 详细使用文档
https://tools.ietf.org/html/rfc4749 git://git.linphone.org/linphone-android.git http://stackoverflow ...
- macOS 上安装 PECL
一.简介 PECL(The PHP Extension Community Library)是 PHP 扩展的存储库,为 PHP 所有的扩展提供提供托管和下载服务. 通过 PEAR(PHP Exten ...
- Elment UI的使用说明
一. Elment UI 1. 简介 Element UI是饿了么团队提供的一套基于Vue2.0的组件库,可以快速搭建网站,提高开发效率,就如同bootstrap. 2.组件分类 ElementUI ...
- Java中的编码乱码问题
1. Eclipse的Run Configurations中,可以配置Console的Encoding Eclipse中使用 mvn clean package命令来执行. 设置为MS932时,下面的 ...
- python字符串搜索
python字符串字串查找 find和index方法 更多0 python 字符串 python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 ...
- css3有哪些新特性
转载:http://blog.csdn.net/lxcao/article/details/52797914
- 写几个简单用artTemplate的例子
写几个简单的artTemplate的例子,很多框架都有自己的模板(template),没得时候,可以利用artTemplate.js完成 html文件是: <!DOCTYPE html> ...