当前位置:网站首页>(2022 Niuke multi School II) l-link with level editor I (dynamic planning)
(2022 Niuke multi School II) l-link with level editor I (dynamic planning)
2022-07-25 05:45:00 【AC__ dream】
subject :
Examples 1 Input :
3 3
1
2 1
1
2 3
1
1 2Examples 1 Output :
-1Examples 2 Input :
3 3
1
2 1
1
1 2
1
2 3Examples 2 Output :
2The question : Yes n A world , Every world has n A little bit , Number from 1~n,m side , We can choose a sub segment from these worlds to play , The rule is from any world 1 set out , Each world can stay where it is or go through one side to another , After this operation, it will be directly transmitted to the point with the same number in the next world , Arrival point m It's victory . It is required to select a sub paragraph as short as possible , So that there is at least one solution that can win , If you can't get there m Click to output -1.
Be careful :
(1) The final answer we seek is the number of worlds the path passes through , Instead of path length and
(2) Memory is very small , Can't build a map to do
(3) From any world 1 Starting at No
(4) You can only go from the world with smaller number to the world with larger number
analysis : set up f[i][j] It means to arrive at the third i A world j The minimum number of worlds the path of a point passes through , So the answer min(f[i][m])(1<=i<=n)
The update is also relatively simple , The first i A world j Point can be determined by i-1 A world j O 'clock , It can also be determined by i-1 A world u O 'clock , The premise is that there is an edge (u,j)
One thing to note is that For any layer i, There are f[i][1]=0, Then we can directly change the read in side to update , Record in the process f[i][m] The minimum value of .
The above idea is correct , But due to memory problems , Therefore, two-dimensional arrays cannot be implemented , It can only be achieved by rolling array optimization , As we update the array of the current world, we only need to use the array of the previous world , So we just need to open two arrays to record the data of the current layer and the previous layer respectively , namely :f[i] The record is to arrive at the current world i The minimum number of transfers required for a point ,g[i] The record is to arrive at the last world i The minimum number of transfers required for a point , In this way, we can iterate through the rolling array to find the answer .
Here is the code :
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e4+10;
int f[N],g[N];// These two arrays are equivalent to rolling arrays
//f[i] The record is to arrive at the current world i The minimum number of transfers required for a point
//g[i] The record is to arrive at the last world i The minimum number of transfers required for a point
int main()
{
int n,m;
cin>>n>>m;
int ans=0x3f3f3f3f;
memset(g,0x3f,sizeof g);
g[1]=0;
for(int i=1;i<=n;i++)
{
memset(f,0x3f,sizeof f);
int t,u,v;
scanf("%d",&t);
for(int j=1;j<=t;j++)// Through the last world u Go to v Click and send to the current world
{
scanf("%d%d",&u,&v);
f[v]=min(f[v],g[u]+1);
}
for(int i=1;i<=n;i++)// The point of the last world doesn't move directly , Directly to the current world
f[i]=min(f[i],g[i]+1);
ans=min(ans,f[m]);
memcpy(g,f,sizeof f);// Array scrolling
g[1]=0;// From any world 1 It starts at the fifth
}
if(ans!=0x3f3f3f3f) printf("%d",ans);
else printf("-1");
return 0;
}边栏推荐
- R language data The table package performs aggregation transforms of data packets and calculates the grouping interquartile range (IQR) of dataframe data
- Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络
- What are the ways to realize web digital visualization?
- easyrecovery免费数据恢复工具操作简单一键恢复数据
- R language ggpubr package ggarrange function combines multiple images and annotates_ Figure add annotation, annotation, annotation information for the combined image, and use the right parameter to ad
- Summary of common attributes of flex layout
- (2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
- HTB-Devel
- New discovery of ROS callback function
- Mechanism and principle of multihead attention and masked attention
猜你喜欢

10、渲染基础

HTB-Optimum

Unity accesses chartandgraph chart plug-in

Productivity tool in the new era -- flowus information flow comprehensive evaluation

C编程 --“最大子数组的和” 的动态规划的解法

Mechanism and principle of multihead attention and masked attention

CCID released the "Lake warehouse integrated technology research report", and Jushan database was selected as a typical representative of domestic enterprises
![(15)[驱动开发]过写拷贝](/img/1c/68dfff5671add1fe91567e4df34135.png)
(15)[驱动开发]过写拷贝

The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet

Working principle and precautions of bubble water level gauge
随机推荐
Matlab drawing example: 5: Biaxial graph
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
QT qtextedit setting qscrollbar style sheet does not take effect solution
对于von Mises distribution(冯·米塞斯分布)的一点心得
MATLAB作图实例:5:双轴图
ABC 261.D - Flipping and Bonus ( DP )
基于ISO13209(OTX)实现EOL下线序列
C编程 --“最大子数组的和” 的动态规划的解法
npx和npm区别
PHP warehouse inventory management system source code WMS source code
A little experience about von Mises distribution
uniapp手机端uView的u-collapse组件高度init
剑指 Offer 54. 二叉搜索树的第k大节点
Introduction to interface in SystemVerilog
background
Programming hodgepodge (I)
Arm PWN basic tutorial
PMP Exam is easy to confuse concept discrimination skills! Don't lose points after reading!
HTB-Devel
Detailed explanation of stepn chain game system development mode (Sports money making mode)