CodeForces 781D Axel and Marston in Bitland DP
题意:
有一个\(n\)个点\(m\)条边的无向图,边有两种类型,分别用\(0\)和\(1\)标识
因此图中的任意一条路径都对应一个\(01\)字符串
定义一个无限长的字符串\(s\):
开始令\(s'=0\),然后将\(s'\)的反串\(\bar{s'}\)拼到后面得到\(s' \bar{s'}\),如此反复最终得到\(s\)
求从起点出发,在串\(s\)上走最多能走多少步
分析:
令\(arrive(i,t,u)\)表示从点\(u\)出发一共走了\(2^i\)步所能到达的点的集合,其中\(t=0\)表示在\(s\)上走,\(t=1\)表示在串\(\bar{s}\)上走
假设在\(s\)上有个\(u \to v\)长为\(2^i\)的路径,在\(\bar s\)上有个\(v \to w\)长为\(2^i\)的路径,那么拼起来在\(s\)上就有一条\(u \to w\)长为\(2^{i+1}\)的路径
所以\(arrive(i+1,t,u)=\bigcup\limits_{v \in arrive(i,t,u)} arrive(i,1-t,v)\)
再令\(go(i, t, u)\)表示第\(i\)轮迭代后,从点\(u\)出发在串\(s\)或\(\bar s\)上最多走多少步
如果\(v \in arrive(i,t,u)\),那么就可以用\(2^i + arrive(i,1-t,v)\)去更新\(go(i+1,t,u)\),相当于把两段路拼起来
#include <cstdio>
#include <bitset>
using namespace std;
const int maxn = 500;
const int maxlog = 61;
typedef long long LL;
LL go[maxlog][2][maxn];
bitset<maxn> arrive[maxlog][2][maxn];
void max(LL& a, LL b) { if(b > a) a = b; }
int main()
{
int n, m; scanf("%d%d", &n, &m);
while(m--) {
int u, v, t; scanf("%d%d%d", &u, &v, &t);
u--; v--;
go[0][t][u] = 1;
arrive[0][t][u][v] = 1;
}
for(int i = 0; i + 1 < maxlog; i++) {
for(int t = 0; t < 2; t++) {
for(int u = 0; u < n; u++) {
go[i+1][t][u] = go[i][t][u];
for(int v = 0; v < n; v++) if(arrive[i][t][u][v]) {
arrive[i+1][t][u] |= arrive[i][t^1][v];
max(go[i+1][t][u], (1LL << i) + go[i][t^1][v]);
}
}
}
}
const LL limit = 1000000000000000000LL;
LL& ans = go[maxlog - 1][0][0];
if(ans > limit) puts("-1");
else printf("%lld\n", ans);
return 0;
}
CodeForces 781D Axel and Marston in Bitland DP的更多相关文章
- Codeforces 781D Axel and Marston in Bitland
题目链接:http://codeforces.com/contest/781/problem/D ${F[i][j][k][0,1]}$表示是否存在从${i-->j}$的路径走了${2^{k}} ...
- Codeforces 781D Axel and Marston in Bitland 矩阵 bitset
原文链接https://www.cnblogs.com/zhouzhendong/p/CF781D.html 题目传送门 - CF781D 题意 有一个 n 个点的图,有 m 条有向边,边有两种类型: ...
- CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
Axel and Marston in Bitland 好开心第一次补$F$题虽然是$Div.2$ 题意: 一个有向图,每条边是$0$或$1$,要求按如下规则构造一个序列然后走: 第一个是$0$,每次 ...
- 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] ...
- codeforces781D Axel and Marston in Bitland
题目链接:codeforces781D 正解:$bitset$+状压$DP$ 解题报告: 考虑用$f[t][0.1][i][j]$表示从$i$出发走了$2^t$步之后走到了$j$,且第一步是走的$0$ ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- codeforces 425C Sereja and Two Sequences(DP)
题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...
随机推荐
- juypter-notebook安装配置
juypter-notebook安装配置 Table of Contents 1. jupyter notebook概述 2. jupyter notebook安装 3. 在jupyter noteb ...
- httpclient通过post提交到webapi
var client = new HttpClient(); var url = BASConfig.Instance.SiteSettingsModule.SyncWorkLogAppUrl; va ...
- 二、React中的更新
创建更新的方式:ReactDOM.render||hydrate 初次渲染,然后是setState.forceUpdate 一.React.render() 步骤:创建ReactRoot.创建Fibe ...
- vue使用element-ui实现按需引入
基于Vue的Ui框架 饿了么公司基于vue开的的vue的Ui组件库 Element Ui 基于vue pc端的UI框架 MintUi 基于vue 移动端的ui框架 http://element.ele ...
- Miner3D Enterprise 企业版
——极致的视觉数据挖掘和知识发现解 Miner3D Enterprise 企业版是一个有力的综合数据驱动的三维可视化和数据分析解决方案.金融师.药剂师.生物技术或化学材质科研人员.地质学家.石油开采专 ...
- Eucalyptus-利用镜像启动一个Windows Server 2008r2实例
1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...
- font:inherit
font:inherit 字体的设置 设置所有元素的字体保持一致: 所有元素:*{font:inherit;} /* IE8+ */ body体用percent:body{font:100%/1 sa ...
- python property用法
参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...
- SQL重复记录查询-count与group by having结合查询重复记录
查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select peopleId from p ...
- linux 命令——48 watch (转)
watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...