1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int Mn(55); inline int _min(int x,int y) { return x<y?x:y; } int n; int pos[Mn],spw[Mn]; int d[Mn][Mn][2]; int pwr(int l,int r) { return spw[l-1]+spw[n]-spw[r]; } int f(int l,int r,int isr) { if(d[l][r][isr]!=-1) return d[l][r][isr]; else { if(!isr) { int ret(0x3f3f3f3f); ret = _min(ret,f(l+1,r,0)+pwr(l+1,r)*(pos[l+1]-pos[l])); ret = _min(ret,f(l+1,r,1)+pwr(l+1,r)*(pos[r]-pos[l])); return d[l][r][isr] = ret; } else { int ret(0x3f3f3f3f); ret = _min(ret,f(l,r-1,0)+pwr(l,r-1)*(pos[r]-pos[l])); ret = _min(ret,f(l,r-1,1)+pwr(l,r-1)*(pos[r]-pos[r-1])); return d[l][r][isr] = ret; } } } int main() { int c; scanf("%d%d",&n,&c); for(int i(1);i<=n;++i) { scanf("%d%d",pos+i,spw+i); spw[i] += spw[i-1]; } memset(d,-1,sizeof d); for(int i(1);i<=n;++i) d[i][i][0] = d[i][i][1] = 0x3f3f3f3f; d[c][c][0] = d[c][c][1] = 0; cout << _min(f(1,n,0),f(1,n,1)); return 0; }
|