NOJ——[1372] Do What

  • 问题描述
  • There are n numbers of business, different business will cost you different times. But you need not to finish all of them, just it is ok when you finish some of them that you spend more than T minutes(do not include T minutes). 
    So, can you find the way to cost the minimum time when pass T times? 
  • 输入
  • Input until EOF.
    First line will contain a positive integer n(1 <= n <= 100) means the number of businesses. 
    Next line follows n positive integers ti(1 <= ti <= 10), means the time of each business you will cost. 
    Last line is a integer T(1 <= T <= 1000).
  • 输出
  • The minimum time of you cost, if there is no answer, print '-1'.
  • 样例输入
  • 3
    1 2 3
    10
    3
    1 4 7
    6
    
  • 样例输出
  • -1
    7
    
  • 提示
  • 来源
  • Hungar
01背包。水题

#include<stdio.h>
#include<string.h>
#define inf -0x3f3f3f

int dp[10010];

int val[105];

int max(int a,int b)
{
return a>b?a:b;
}

int main()
{
int n;
while(~scanf("%d",&n))
{
int sum=0,t;
for(int i=0;i<n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
}
scanf("%d",&t);
if(sum<=t)
printf("-1\n");
else
{
int v=t+1;
while(1)
{
memset(dp,inf,sizeof(dp));
dp[0]=0;
for(int i=0;i<n;i++)
for(int j=v;j>=val[i];j--)
dp[j]=max(dp[j],dp[j-val[i]]+val[i]);
if(dp[v]!=inf)
break;
else
v++;
}
printf("%d\n",v);
}
}
return 0;
}


你可能感兴趣的:(NOJ——[1372] Do What)