http://acm.timus.ru/problem.aspx?space=1&num=1342
简单DP
代码:
#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<set> #include<map> #include<string> #include<queue> #include<stack> #include <iomanip> using namespace std; #define LL long long const int INF=0x3f3f3f3f; const double FINF=1e12; const int N=1005; double ans[N][N]; int k[N]; double p[N],q[N]; int main() { //freopen("data.in","r",stdin); int n,m; while(cin>>n>>m) { for(int i=1;i<=n;++i) cin>>k[i]>>p[i]>>q[i]; for(int i=0;i<=n;++i) for(int j=0;j<=m;++j) if(j==0) ans[i][j]=0.0; else ans[i][j]=FINF; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) { double sum=0.0; double t=0.0; if(k[i]>1) t=(q[i]-p[i])/(k[i]-1); for(int l=0;l<=k[i]&&l<=m;++l) { if(l>0) sum+=(p[i]+(l-1)*t); ans[i][j]=min(ans[i][j],sum+ans[i-1][j-l]); } } if(ans[n][m]<FINF) printf("Minimum possible cost: %.2f\n",ans[n][m]); else { int w; for(w=m;w>=0;--w) if(ans[n][w]<FINF) break; printf("Maximum possible amount: %d\n",w); printf("Minimum possible cost: %.2f\n",ans[n][w]); } } return 0; }