标签:
///1085422276 #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <queue> #include <map> #include <stack> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define pb push_back #define meminf(a) memset(a,127,sizeof(a)); inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘)f=-1; ch=getchar(); } while(ch>=‘0‘&&ch<=‘9‘) { x=x*10+ch-‘0‘; ch=getchar(); } return x*f; } //**************************************** #define maxn 100000+50 #define mod 1000000007 #define inf 1000000007 ll a[1001][1001],b[maxn],k,dp[1001][1001],l[maxn],r[maxn],ans[maxn],D[maxn],n,m; int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%I64d",&a[i][j]);dp[i][j]=inf; } } dp[1][1]=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==1&&j==1) { dp[i][j+1]=min(dp[i][j]+a[i][j]*a[i][j+1],dp[i][j+1]); dp[i+1][j] =min( dp[i][j]+a[i][j]*a[i+1][j],dp[i+1][j]); } else { dp[i][j+2]=min(dp[i][j]+a[i][j+1]*a[i][j+2],dp[i][j+2]); dp[i+2][j] =min( dp[i][j]+a[i+1][j]*a[i+2][j],dp[i+2][j]); dp[i+1][j+1]=min(dp[i][j]+a[i+1][j]*a[i+1][j+1],dp[i+1][j+1]); dp[i+1][j+1] =min( dp[i][j]+a[i][j+1]*a[i+1][j+1],dp[i+1][j+1]); } } } cout<<dp[n][m]<<endl; } return 0; }
HDU5569/BestCoder Round #63 (div.2) C.matrix DP
标签:
原文地址:http://www.cnblogs.com/zxhl/p/4984945.html