Describe the vertical tunnel in which the adventure team enters the elevator after the power of the elevator is enabled, A track that passes through the top of the tower, an elevator stops at the bottom of the track, and a huge handle in the elevator that controls the lift.
The tower of Nescafe has a total of N layers, and the elevator has a stop point on each layer. The handle has m control slots, and a number Ci is placed next to the I control slots to meet the requirements of C1 <C2 <C3 <...... <Cm. If Ci> 0, it indicates that when the handle is pulled to the slot, the elevator will rise to the CI layer; If Ci <0, it means that when the handle is pulled to the slot, the elevator will fall to the CI layer; and there must be a CI = 0, and the handle is originally located in this slot. Note that the elevator can only be 1 ~ N layers move, so it is not allowed to pull the ladder to the control slot that moves below 1 layer and above N layers.
It takes 2 seconds for an elevator to move each floor, and 1 second for the handle to pull from a control slot to an adjacent slot. Adventure players are now on Layer 1 and want to arrive at Layer N as soon as possible. They want to know how long it will take from Layer 1 to layer n? Question: Is it said that the positive solution is stratified spfa? It seems feasible. I directly had a brainless DP, an infinite loop, and an infinite loop because DP had no explicit relationship and had to go wrong... Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 200014 #define maxm 10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26 int x=0,f=1;char ch=getchar();27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}29 return x*f;30 }31 int n,m,p,a[maxn],f[maxn][maxm];32 int main()33 {34 freopen("input.txt","r",stdin);35 freopen("output.txt","w",stdout);36 n=read();m=read();37 for1(i,m)a[i]=read();38 for1(i,n)for1(j,m)f[i][j]=inf;39 for1(i,m)if(!a[i])p=i;40 for1(i,m)f[1][i]=abs(i-p);41 bool flag=1;42 while(flag)43 {44 flag=0;45 for3(i,n,1) 46 for1(j,m)47 {48 int k=i-a[j];49 if(k>n||k<1)continue;50 for1(l,m)51 if(f[k][l]+abs(l-j)+abs(a[j])*2<f[i][j])flag=1,f[i][j]=f[k][l]+abs(l-j)+abs(a[j])*2;52 }53 //for1(i,n)for1(j,m)cout<<i<<‘ ‘<<j<<‘ ‘<<f[i][j]<<endl;54 }55 int ans=inf;56 for1(i,m)ans=min(ans,f[n][i]);57 printf("%d\n",ans>=inf/2?-1:ans);58 return 0;59 }
View code
P2032 "poetize9" elevator