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 看懂之后纠结好久...不会做...仍然是看题解 ...
随机推荐
- SpringMVC 返回自定义属性名
SpringMVC 返回的属性名默认是小写驼峰形式的实体对象中的属性名,如 userID 属性名它会返回 userId. 如果接口方式之前已经定下来,这样前端按原来的方式取数据会读取失败的,那有没有方 ...
- jquery jquery选择器总结 转自(永远的麦子)
jQuery选择器总结 阅读目录 1, 基本选择器? 2, 层次选择器? 3, 过滤选择器? 4, 表单选择器? jQuery选择器共有四大类,分别为基本选择器,层次选择器,过滤选择器和表单选择器.下 ...
- 关于在C++中调用system函数
先看看下面的这一段程序: #include <iostream> #include <cstdlib> int main(int argc, char* argv[]) { s ...
- C#Udp组播
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- ubuntu14.04server下安装scala+sbt工具
安装sbt参考https://www.cnblogs.com/wrencai/p/3867898.html 在安装scala时 首先得安装jdk环境,最好安装最新版本以免后续安装出现不必要的麻烦 一. ...
- Sentinel spring-cloud-gateway adapter(1.6)异常错误之@EnableCircuitBreaker
sentinal 大坑 使用gateway adaper包出现@EnableCircuitBreaker did you forget include starter的异常 这时候千万不要学我引入cl ...
- hangfire使用
1 . NuGet 命令行执行 Install-Package Hangfire2.首先在ConfigureServices 方法中注册服务: services.AddHangfire(r=>r ...
- python_55_局部和全局变量
school='Hebut'#school为全局变量 sex='male'#全局变量 names=['Wang Yu','Bai Jingyi','Zhang Yu'] hobby='姑娘' def ...
- PageHelper 记录总条数不正确问题处理
//PageHelper.startPage会返回一个page对象,这个对象在查询结果出来后会把页数,记录总数给page对象,用page.getPages()和getTotal()获取页数和记录总数. ...
- Drupal常用的模块
CCK (Content Construction Kit ) : 添加字段模块 Views:生成列表 Tinymce:(Wysiwyg Editor) 常用的编辑器之一 Ajax Form Buil ...