syhd142  
日历
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
统计
  • 随笔 - 23
  • 文章 - 122
  • 评论 - 31
  • 引用 - 0

导航

常用链接

留言簿(2)

随笔档案(23)

文章分类(270)

文章档案(122)

我的豆瓣

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
0/1背包问题,和624类似,只是这次价值和花费不同,这次同样需要输出选择的物品。
输出恶心到我了,竟然PE两次。
#include <stdio.h>
#include 
<string.h>

#define T 1005
#define N 35

struct Treature
{
    
int depth, time, value;
}treature[N];

int c[T];
bool p[N][T];

int main()
{
    
int t, w, n, count, cas = 0;
    
while(~scanf("%d %d"&t, &w))
    {
        
if(cas) puts("");
        scanf(
"%d"&n);
        
for(int i = 1; i <= n; i++)
        {
            
int depth, value;
            scanf(
"%d %d"&depth, &value);
            treature[i].depth 
= depth;
            treature[i].time 
= 3 * depth * w;
            treature[i].value 
= value;
        }
        
        memset(c, 
0sizeof(c));
        memset(p, 
0sizeof(p));
        
        
for(int i = n; i; i--)
        
for(int j = t; j >= treature[i].time; j--)
        {
            
if(c[j] < c[j - treature[i].time] + treature[i].value)
            {
                c[j] 
= c[j - treature[i].time] + treature[i].value;
                p[i][j] 
= 1;
            }
        }
        printf(
"%d\n", c[t]);
        count 
= 0;
        
for(int i = 1, j = t; i <= n; i++)
            
if(p[i][j])
            {
                count
++;
                j 
-= treature[i].time;
            }
        printf(
"%d\n", count);
        
for(int i = 1, j = t; i <= n; i++)
            
if(p[i][j])
            {
                printf(
"%d %d\n", treature[i].depth, treature[i].value);
                j 
-= treature[i].time;
            }
        cas
++;
    }
    
return 0;
}
posted on 2010-06-30 11:31 Fucker 阅读(407) 评论(0)  编辑 收藏 引用 所属分类: ACM/ICPCDP

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © Fucker Powered by: 博客园 模板提供:沪江博客