Input
The input consists of multiple test cases, each test case starts with a number nnn (1≤n≤1031 \le n \le 10^31≤n≤103) -- the number of types, and mmm (1≤m≤1041 \le m \le 10^41≤m≤104) -- the least kilograms of water he needs to buy. For each set of test cases, the sum of nnn does not exceed 5e45e45e4.
Then followed n lines with each line two integers ppp (1≤p≤1091 \le p \le 10^91≤p≤109) -- the price of this type, and ccc (1≤c≤1041 \le c \le 10^41≤c≤104) -- the weight of water this type contains.
Output
For each test case, you should output one line contains the minimum cost aaa and the weight of water Dawn-K will get bbb. If this minimum cost corresponds different solution, output the maximum weight he can get.
(The answer aaa is between 111 and 10910^9109, and the answer bbb is between 111 and 10410^4104)
样例输入
3 3 2 1 3 1 1 1 3 5 2 3 1 2 3 3
样例输出
3 3 3 6
题意:给你n种水,每种水有价值x,重量y,问买m千克的水至少要花多少钱
思路:完全背包改改就可以过了
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int dp[1050010];
int c[1050010],w[1050010];
int main()
{
int m,v;
while(scanf("%d%d",&m,&v)!=EOF)
{
int v1=v;
for(int i=0; i<m; i++)
{
scanf("%d%d",&c[i],&w[i]);
}
v=1000;
v*=11;
memset(dp,INF,sizeof(dp));
dp[0]=0;
for(int i=0; i<m; i++)
for(int j=w[i]; j<=v; j++)
dp[j]=min(dp[j],dp[j-w[i]]+c[i]);
int tempk=INF,tempg=0;
for(int i=v1;i<=v;i++)
{
if(tempk>=dp[i])
{
tempk=dp[i];
tempg=i;
}
//printf("dp[%d]=%d\n",i,dp[i]);
}
printf("%d %d\n",tempk,tempg);
}
return 0;
}