当前位置:网站首页>L. Link with Level Editor I dp
L. Link with Level Editor I dp
2022-07-26 05:48:00 【Strezia】
L
dp
题意
有 n n n 个世界,每个世界是一张简单有向图。从这些世界中选择一个子段进行游戏,规则为从 1 1 1 出发,每个世界可以原地不动或经过一条边,到达点 m m m 即为胜利。
要求选出一个尽可能短的子段,使得存在至少一种方案可以胜利。
思路
记 d p [ i ] [ j ] dp[i][j] dp[i][j] 表示到第 i i i 个世界中的 j j j 点最少需要经过多少个世界。答案即为 d p [ n ] [ m ] dp[n][m] dp[n][m] 。
考虑如何在第 i i i 个世界中到达 v v v ,只有如下两种转移方式:
- 通过 u , v u,v u,v 从第 i − 1 i-1 i−1个世界中的 u u u 到达 v v v 。 d p [ i ] [ v ] = d p [ i − 1 ] [ u ] + 1 dp[i][v]=dp[i-1][u]+1 dp[i][v]=dp[i−1][u]+1
- 不动,从第 i − 1 i-1 i−1 个世界中的 v v v 到达 v v v 。 d p [ i ] [ v ] = d p [ i − 1 ] [ v ] + 1 dp[i][v]=dp[i-1][v]+1 dp[i][v]=dp[i−1][v]+1
所以只需要依次对每个世界枚举所有边,用滚动数组优化即可。注意起点必须是1。
代码
int n, m;
int dp[2][maxm]; //dp[i][j] 表示到第i个世界的第j个点最少要经过多少个世界
void solve() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
dp[0][i] = dp[1][i] = INF;
}
int ans = INF;
int p = 0;
for(int i = 1; i <= n; i++) {
int e;
cin >> e;
p ^= 1;
for(int j = 1; j <= m; j++) {
dp[p][j] = dp[p^1][j] + 1;
}
for(int j = 1; j <= e; j++) {
int u, v;
cin >> u >> v;
if(u == 1) {
dp[p][v] = 1;
}
else {
chmin(dp[p][v], dp[p^1][u] + 1);
}
}
chmin(ans, dp[p][m]);
}
if(ans == INF) {
cout << -1 << endl;
}
else {
cout << ans << endl;
}
}
边栏推荐
- 数仓搭建-DIM层
- Unity Profiler
- NFT in the eyes of blackash: the platform is crying for slaughter, and users send money to the door
- Redis持久化-RDB
- Balanced binary tree (AVL)~
- Redis persistence AOF
- [paper notes] anti packet loss joint coding for network speech steganography
- CANoe-XML在Test Modules中的应用
- Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
- C language explanation series -- understanding of functions (3) formal parameters, arguments, nested calls and chain access
猜你喜欢

The idea YML file code does not prompt the solution

高频电子线路复习考试题及答案

How students apply for free idea

Solution to slow download speed of vagrant

Introduction to Chinese text error correction task

I also found excellent software and hardware projects, all open source

Is it really hopeless to choose electronic engineering and be discouraged?

SSH Remote Management

Why can't lpddr completely replace DDR?

Redis发布订阅
随机推荐
Knowledge points of Polymer Physics
Chapter 2 - getting started
CANoe-XML在Test Modules中的应用
Motor control column summary
10. Regular expression matching
某公司给每个工位装监控:只为看员工写代码?
ES Cluster in Red status: what about write & delete operations?
High frequency electronic circuit review examination questions and answers
Nn.moudle module - details of creating neural network structure
日志收集分析平台搭建-1-环境准备
517. Super washing machine
Mongondb API usage
Two auxiliary functions of integral Mall for business user operation
MBA-day28 数的概念-练习题
Unity Profiler
520送什么?DIY一个高颜值RGB时钟,女生看了都想要
Redis official visualization tool, with high appearance value and powerful functions!
NFT in the eyes of blackash: the platform is crying for slaughter, and users send money to the door
高分子物理知识点
金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)