sdau三 1018
2016-05-31 08:26
337 查看
问题:
有一扑满,有多种硬币,每种硬币价值重量不同,每种硬币数量无限。要把扑满装满,求扑满中的最少能装多少钱。
input:
输入n,后有n组事例,输入空扑满和装满钱的扑满的重量,输入m,后有m组硬币,各有价值v与重量w
out:
输出最小价值或输出不可能(若装不满)
simple input:
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
simple output:
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.
分析:
一定要装满的完全背包问题。求最小值,所以关键是要将dp数组初始化为正无穷,然后套完全背包模板就好
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int p,w,dp[10001];
const int INF=100000000000;
int main(){
//freopen("s.txt","r",stdin);
int t;
cin>>t;
while(t--){
int s1,s2,s,n,i,j;
cin>>s1>>s2;
s=s2-s1;
cin>>n;
for(i=1;i<=s;i++)
dp[i]=INF;
for(i=1;i<=n;i++){
cin>>p>>w;
for(j=w;j<=s;j++)
dp[j]=min(dp[j],dp[j-w]+p);
}
if(dp[s]!=INF)
cout<<"The minimum amount of money in the piggy-bank is "<<dp[s]<<"."<<endl;
else
cout<<"This is impossible."<<endl;
}
return 0;
}
有一扑满,有多种硬币,每种硬币价值重量不同,每种硬币数量无限。要把扑满装满,求扑满中的最少能装多少钱。
input:
输入n,后有n组事例,输入空扑满和装满钱的扑满的重量,输入m,后有m组硬币,各有价值v与重量w
out:
输出最小价值或输出不可能(若装不满)
simple input:
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4
simple output:
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.
分析:
一定要装满的完全背包问题。求最小值,所以关键是要将dp数组初始化为正无穷,然后套完全背包模板就好
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int p,w,dp[10001];
const int INF=100000000000;
int main(){
//freopen("s.txt","r",stdin);
int t;
cin>>t;
while(t--){
int s1,s2,s,n,i,j;
cin>>s1>>s2;
s=s2-s1;
cin>>n;
for(i=1;i<=s;i++)
dp[i]=INF;
for(i=1;i<=n;i++){
cin>>p>>w;
for(j=w;j<=s;j++)
dp[j]=min(dp[j],dp[j-w]+p);
}
if(dp[s]!=INF)
cout<<"The minimum amount of money in the piggy-bank is "<<dp[s]<<"."<<endl;
else
cout<<"This is impossible."<<endl;
}
return 0;
}
相关文章推荐
- 2016SDAU课程练习三1020
- (二)boost库之字符串格式化
- (二)boost库之字符串格式化
- (一)boost库之日期、时间
- (一)boost库之日期、时间
- SQL2008R2 不支持用该后端版本设计数据库关系图或表
- Boost库学习之旅入门篇
- Microservices Reference Architecture - with Spring Boot, Spring Cloud and Netflix OSS--转
- Boost库学习之旅入门篇
- PAT 甲级 1027 Colors in Mars
- PAT 甲级 1027 Colors in Mars
- JavaScript也谈内存优化
- C# 3.0新语言特性和改进(一)
- vsftpd配置文件详解
- PAT 甲级 1026 Table Tennis(模拟)
- adapter layout 优化
- PAT 甲级 1026 Table Tennis(模拟)
- linux内核学习之进程管理------task_struct结构体
- windows用windeployqt发布qt quick application程序
- Jquery toggleClass的使用