标签:
证明w满足四边形不等式,这里w是m的附属量,形如m[i,j]=opt{m[i,k]+m[k,j]+w[i,j]},此时大多要先证明w满足条件才能进一步证明m满足条件
证明m满足四边形不等式
证明s[i,j-1]≤s[i,j]≤s[i+1,j]
#include<cstdio> #include<cstring> #include<cctype> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #define clr(x,c) memset(x,c,sizeof(x)) #define ll long long int read(){ int x=0;char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) x=x*10+c-‘0‘,c=getchar(); return x; } const int nmax=2e3+5; const ll inf=1e17; ll a[nmax],dp[nmax][nmax],s[nmax][nmax]; void mins(ll &a,ll b){ if(a>b) a=b; } int main(){ int n=read();rep(i,1,n*2) rep(j,1,n*2) dp[i][j]=inf; rep(i,1,n) a[i]=read(); rep(i,1,n) a[i+n]=a[i]; rep(i,1,2*n) a[i]+=a[i-1],dp[i][i]=0,s[i][i]=i; int t,tp; rep(i,1,n-1) rep(j,1,2*n-i) { t=j+i; rep(k,s[j][t-1],s[j+1][t]) { tp=dp[j][k]+dp[k+1][t]+a[t]-a[j-1]; if(tp<dp[j][t]) dp[j][t]=tp,s[j][t]=k; } } ll ans=inf; rep(i,1,n) mins(ans,dp[i][i+n-1]); printf("%lld\n",ans); return 0; }
第1行:N(2 <= N <= 1000) 第2 - N + 1:N堆石子的数量(1 <= A[i] <= 10000)
输出最小合并代价
4 1 2 3 4
19
标签:
原文地址:http://www.cnblogs.com/fighting-to-the-end/p/5878360.html