CSUOJ1630--Plane Ticket Pricing

类似于背包,但是最后物品可以拆分.另外,因为要求第一次的选择,从后往前进行dp.

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

#define INF 0x3f3f3f3f

typedef struct
{
     int cnt;
     int w[103],v[103];
}P;
P p[55];

int dp[55][550];

int main()
{
      //freopen("in.txt","r",stdin);
       int n,m;
       while(scanf("%d%d",&n,&m)!=EOF)
       {
              m++;
              memset(dp,0,sizeof(dp));
              for(int i=m;i>=1;i--)
              {
                    scanf("%d",&p[i].cnt);
                    for(int j=0;j<p[i].cnt;j++)
                         scanf("%d",&p[i].w[j]);
                    for(int j=0;j<p[i].cnt;j++)
                         scanf("%d",&p[i].v[j]);
              }
              memset(dp,-1,sizeof(dp));
              dp[0][0]=0;
              for(int i=1;i<=m;i++)
              {
                  for(int j=n;j>=0;j--)
                  {
                         for(int k=0;k<p[i].cnt;k++)
                         {
                               if(j-p[i].v[k]>=0&&dp[i-1][j-p[i].v[k]]!=-1)
                               {
                                      dp[i][j]=max(dp[i][j],dp[i-1][j-p[i].v[k]]+p[i].w[k]*p[i].v[k]);
                               }
                                if(j==p[i].v[k]) ///可以卖任意多张的票
                                      for(int d=p[i].v[k]-1;d>=0;d--)
                                          if(dp[i-1][0]!=-1)
                                               dp[i][d]=max(dp[i][d],dp[i-1][0]+d*p[i].w[k]);
                          }
                  }
              }
        int sum=0;
        for(int i=0;i<=n;i++)
            sum=max(dp[m][i],sum);
        if(m==1) ///m==1的情况需特判
        {
            printf("%d\n",sum);
            for(int i=0;i<p[m].cnt;i++)
                if(p[m].w[i]*p[m].v[i]==sum)
                {
                      printf("%d\n",p[m].w[i]);
                      break;
                }
            continue;
        }
        printf("%d\n",dp[m][n]);
        for(int i=0;i<p[m].cnt;i++)
              if(dp[m][n]==dp[m-1][n-p[m].v[i]]+p[m].v[i]*p[m].w[i])
              {
                    printf("%d\n",p[m].w[i]);
                    break;
              }
       }
   return 0;
}
时间: 2024-10-15 21:38:20

CSUOJ1630--Plane Ticket Pricing的相关文章

CSU1630: Plane Ticket Pricing

Description Plane ticket prices fluctuate wildly from one week to the next, and their unpredictability is a major source of frustration for travellers. Some travellers regret buying tickets too early when the prices drop right after they purchase the

[2016-1-14]OMG美语每日笔记-?How do you find cheap flight ticket?

坚持学习英语,OMG口语非常长不错,坚持每天整理.学英语坚持最重要,学英语坚持最重要,学英语坚持最重要说三遍! otimal time 最佳时间 I am planing my Spring Festival vacation.What's the otimal time to buy a plane ticket? 我在准备春节旅行.什么时候买票最好? cheap flight tickets 特价机票 There is a rumer that the best time to buy ch

Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/ServletRegistrationBean

异常信息 2017-09-02 18:06:37.223 [main] ERROR o.s.boot.SpringApplication - Application startup failed java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$Endpoint

UVA - 12123 Magnetic Train Tracks

Description The rail roads of Japan are being redesigned. So the governent is planning to install ultra-modern Magnetic trains instead of the current normal trains. As fuel price have gone high and nations have shut down their nuclear plants so the p

新概念 Lesson 2 Sorry, sir.

Is this your handbag? 这是你的手提包吗? Yes,it is. /No it isn't 人称代词的主格宾格 形容性物主代词的用法 Does the man get his umbrella? 这个男子拿到了他的雨伞吗? umbrella n. 伞 n. -> noun: 名词 e.g 我的雨伞: My umbrella 你的雨伞: Your umbrella 他/她的雨伞: his/her umbrella ticket n.票 e.g.  一张电影票: a movie

present simple, continuous, and be going to 三者区别

https://www.youtube.com/watch?v=a03VKQL0dZg&list=PLZOJurmtexYozmvQGAJnVg3lgiZw0mj-y HOW TO USE FUTURE TENSES IN ENGLISH The example I gave at the start of the video was this: "My sister is coming tonight. She gets in at 5:10. I'm picking her up a

Find the total area covered by two rectilinear rectangles in a 2D plane. 208MM

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. pu

poj2828 Buy ticket

Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue… The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he ha

微信第三方平台开头篇--MVC代码(第三方获取ticket和公众号授权)

微信公众号授权给开放平台 公众号授权给第三方平台的技术实现流程比较简单 这个步骤遗漏了开头获取第三方平台自己的accessToken 先说下流程 如何注册开放平台的第三方信息看截图 其他不说了,此文只说代码部分. 先获取第三方10分一次的ticket. 1 using (var streamReader = new StreamReader(Request.InputStream)) 2 { 3 string stringInput = streamReader.ReadToEnd(); 4 s