这题是完全背包问题。看过背包九讲,再搜了下网上大牛们的代码,才有一点小懂!
主要是想贴上的我的四个AC代码,看看对函数的不同处理对运行时间的影响:
①
#include<iostream> using namespace std; #define INF 0x7fffffff int empty,full,n_case; int record[10000]; struct node { unsigned int value; int weight; }a[500]; int main() { int t_case,volume,i,j; cin>>t_case; while(t_case--) { cin>>empty>>full>>n_case; for(i=0;i<n_case;i++) cin>>a[i].value>>a[i].weight; volume=full-empty; record[0]=0; for(i=1;i<=volume;i++) record[i]=INF; for(i=0;i<n_case;i++) { for(j=a[i].weight;j<=volume;j++) { if(record[j-a[i].weight]+a[i].value <record[j]) record[j]=record[j-a[i].weight]+a[i].value; } } if(record[volume]<INF) cout<<"The minimum amount of money in the piggy-bank is "<<record[volume]<<"."<<endl; else cout<<"This is impossible."<<endl; } return 0;}
②
#include<iostream> using namespace std; #define INF 0x7fffffff #define min(a,b) a>b?b:a int empty,full,n_case; int record[10000]; struct node { unsigned int value; int weight; }a[500]; int main() { int t_case,volume,i,j; cin>>t_case; while(t_case--) { cin>>empty>>full>>n_case; for(i=0;i<n_case;i++) cin>>a[i].value>>a[i].weight; volume=full-empty; record[0]=0; for(i=1;i<=volume;i++) record[i]=INF; for(i=0;i<n_case;i++) { for(j=a[i].weight;j<=volume;j++) { record[j]=min(record[j-a[i].weight]+a[i].value,record[j]); } } if(record[volume]<INF) cout<<"The minimum amount of money in the piggy-bank is "<<record[volume]<<"."<<endl; else cout<<"This is impossible."<<endl; } return 0; }
③
#include<iostream> using namespace std; //#define INF 0x7fffffff #define INF 9999999 int empty,full,n_case; int record[10000]; struct node { unsigned int value; int weight; }a[500]; int min(int p,int q) { return p>q?q:p; } int main() { int t_case,volume,i,j; cin>>t_case; while(t_case--) { cin>>empty>>full>>n_case; for(i=0;i<n_case;i++) cin>>a[i].value>>a[i].weight; volume=full-empty; record[0]=0; for(i=1;i<=volume;i++) record[i]=INF; for(i=0;i<n_case;i++) { for(j=a[i].weight;j<=volume;j++) { record[j]=min(record[j-a[i].weight]+a[i].value,record[j]); } } if(record[volume]<INF) cout<<"The minimum amount of money in the piggy-bank is "<<record[volume]<<"."<<endl; else cout<<"This is impossible."<<endl; } return 0; }
④
#include<iostream> using namespace std; //#define INF 0x7fffffff #define INF 9999999 int empty,full,n_case; int record[10000]; struct node { unsigned int value; int weight; }a[500]; inline int min(int p,int q) { return p>q?q:p; } int main() { int t_case,volume,i,j; cin>>t_case; while(t_case--) { cin>>empty>>full>>n_case; for(i=0;i<n_case;i++) cin>>a[i].value>>a[i].weight; volume=full-empty; record[0]=0; for(i=1;i<=volume;i++) record[i]=INF; for(i=0;i<n_case;i++) { for(j=a[i].weight;j<=volume;j++) { record[j]=min(record[j-a[i].weight]+a[i].value,record[j]); } } if(record[volume]<INF) cout<<"The minimum amount of money in the piggy-bank is "<<record[volume]<<"."<<endl; else cout<<"This is impossible."<<endl; } return 0; }
好的吧,这里还有个WA的代码,希望大牛们看到指点一二!
#include<iostream> using namespace std; #define INF 0x7fffffff int min(int p,int q) { return p>q?q:p; } int empty,full,n_case; int record[10000]; struct node { unsigned int value; int weight; }a[500]; int main() { int t_case,volume,i,j; cin>>t_case; while(t_case--) { cin>>empty>>full>>n_case; for(i=0;i<n_case;i++) cin>>a[i].value>>a[i].weight; volume=full-empty; record[0]=0; for(i=1;i<=volume;i++) record[i]=INF; for(i=0;i<n_case;i++) { for(j=a[i].weight;j<=volume;j++) { record[j]=min(record[j-a[i].weight]+a[i].value,record[j]); } } if(record[volume]<INF) cout<<"The minimum amount of money in the piggy-bank is "<<record[volume]<<"."<<endl; else cout<<"This is impossible."<<endl; } return 0; }
以上的WA代码,如下修改下140MS过了。。。
感谢本博文下的评论者的提示。
for(i=0;i<n_case;i++) { for(j=a[i].weight;j<=volume;j++) { if(record[j-a[i].weight]==INF) continue; //这句不加,可能造成数据溢出 record[j]=min(record[j-a[i].weight]+a[i].value,record[j]); } }