当前位置:网站首页>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;
}
}
边栏推荐
- How to view the container name in pod
- 520 for what? DIY is a high-value RGB clock that girls want to watch
- [STM32 series summary] blogger's way to quickly advance STM32 in actual combat (continuous update)
- FTP experiment and overview
- Redis persistence AOF
- 虚拟偶像代言产品出问题谁负责? 且听律师分析
- Knowledge points of Polymer Physics
- Hack the box -sql injection fundamentals module detailed Chinese tutorial
- Unity Profiler
- Data warehouse construction -dim floor
猜你喜欢
随机推荐
QT writing IOT management platform 47 general database settings
程序员如何改善精神内耗?
You'd better not take this kind of project!
Lemon class automatic learning after all
Attack and defense world flatscience
这种项目,最好别接!
No EGL display error resolution
Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
High frequency electronic circuit review examination questions and answers
Redis 官方可视化工具,高颜值,功能真心强大!
flex布局
How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
leetcode-aboutString
Select sort / insert sort / bubble sort
二叉排序树(BST) ~
Mongodb tutorial Chapter 08 comparison operators
leetcode-Array
Usage and common problems of SIP softphone registered with SIP account
Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!
数仓搭建-DIM层







