标签:style blog http color io os ar for sp
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 1000000000 13 #define maxn 2000 14 #define maxm 100 15 #define eps 1e-10 16 #define ll long long 17 #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 1000000007 23 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 }
标签:style blog http color io os ar for sp
原文地址:http://www.cnblogs.com/zyfzyf/p/4051257.html