Java实现 蓝桥杯VIP 算法提高 促销购物

算法提高 促销购物
时间限制:1.0s 内存限制:256.0MB

问题描述
  张超来到了超市购物。
  每个物品都有价格,正好赶上商店推出促销方案。就是把许多东西一起买更便宜(保证优惠方案一定比原价便宜)。物品要买正好的个数,而且不能为了便宜而买不需要的物品。
  张超拿到了优惠方案,和需要购买的物品清单,当然想求出最小的花费。他是信息学选手,自然地想到写个程序解决问题。
输入格式
  第一行促销物品的种类数(0 <= s <= 99)。
  第二行…第s+1 行每一行都用几个整数来表示一种促销方式。
  第一个整数 n (1 <= n <= 5),表示这种优惠方式由 n 种商品组成。
  后面 n 对整数 c 和 k 表示 k (1 <= k <= 5)个编号为 c (1 <= c <= 999)的商品共同构成这种方案。
  最后的整数 p 表示这种优惠的优惠价(1 <= p <= 9999)。也就是把当前的方案中的物品全买需要的价格。
  第 s+2 行这行一个整数b (0 <= b <= 5),表示需要购买 b 种不同的商品。
  第 s+3 行…第 s+b+2 行这 b 行中的每一行包括三个整数:c ,k ,和 p 。
  C 表示唯一的商品编号(1 <= c <= 999),
  k 表示需要购买的 c 商品的数量(1 <= k <= 5)。
  p 表示 c 商品的原价(1 <= p <= 999)。
  最多购买 5*5=25 个商品。
输出格式
  一个整数ans,表示需要花的最小费用
样例输入
2
1 7 3 5
2 7 1 8 2 10
2
7 3 2
8 2 5
样例输出
14

import java.util.Hashtable;
import java.util.Scanner;
public class Main {//促销购物
	private plan plan[];
	private Hashtable<Integer, Integer> ha=new Hashtable<Integer, Integer>();
	private static int pl[][],n,m;
	private Node t[][][][][][];
	private static boolean b[];

	public void setT(int a,int b,int c,int d,int e,int f) {
		t=new Node[a][b][c][d][e][f];
	}

	public void setPlan(int n) {
		plan=new plan[n];
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		Main ps=new Main();
		int i,j;
		n=Integer.parseInt(sc.next());
		b=new boolean[n];
		ps.setPlan(n);
		for(i=0;i<n;i++)
		{
			ps.plan[i]=ps.new plan();
			m=Integer.parseInt(sc.next());
			ps.plan[i].n=m;
			ps.plan[i].p=new int[m][2];
			for(j=0;j<m;j++)
			{
				ps.plan[i].p[j][0]=Integer.parseInt(sc.next());
				ps.plan[i].p[j][1]=Integer.parseInt(sc.next());
			}
			ps.plan[i].cost=Integer.parseInt(sc.next());
		}
		m=Integer.parseInt(sc.next());
		pl=new int[5][3];
		for(i=0;i<m;i++)
		{
			pl[i][0]=Integer.parseInt(sc.next());
			pl[i][1]=Integer.parseInt(sc.next());
			pl[i][2]=Integer.parseInt(sc.next());
			ps.ha.put(pl[i][0],i);
		}
		ps.setT(n, pl[0][1]+1, pl[1][1]+1, pl[2][1]+1, pl[3][1]+1, pl[4][1]+1);
		if(n!=0)i=fina(0,pl[0][1],pl[1][1],pl[2][1],pl[3][1],pl[4][1],ps);
		else
		{
			i=0;
			for(j=0;j<m;j++)i+=pl[j][1]*pl[j][2];
		}
		System.out.println(i);
	}
	
	private static int fina(int x, int a1, int a2, int a3, int a4, int a5,Main ps) {
		// TODO Auto-generated method stub
		if(ps.t[x][a1][a2][a3][a4][a5]!=null)return ps.t[x][a1][a2][a3][a4][a5].n;
		else
		{
			ps.t[x][a1][a2][a3][a4][a5]=ps.new Node();
			if(b[x])
			{
				if(x==n-1)return ps.t[x][a1][a2][a3][a4][a5].n=a1*pl[0][2]+a2*pl[1][2]+a3*pl[2][2]+a4*pl[3][2]+a5*pl[4][2];
				else return ps.t[x][a1][a2][a3][a4][a5].n=fina(x+1, a1, a2, a3, a4, a5, ps);
			}
			else
			{
				int i,c[],y;
				c=new int[5];
				c[0]=a1;c[1]=a2;c[2]=a3;c[3]=a4;c[4]=a5;
				for(i=0;i<ps.plan[x].n;i++)
				{
					if(!ps.ha.containsKey(ps.plan[x].p[i][0]))
					{
						b[x]=true;
						if(x==n-1)return ps.t[x][a1][a2][a3][a4][a5].n=a1*pl[0][2]+a2*pl[1][2]+a3*pl[2][2]+a4*pl[3][2]+a5*pl[4][2];
						else return ps.t[x][a1][a2][a3][a4][a5].n=fina(x+1, a1, a2, a3, a4, a5, ps);
					}
					else
					{
						y=ps.ha.get(ps.plan[x].p[i][0]);
						c[y]-=ps.plan[x].p[i][1];
						if(c[y]<0)
						{
							if(x==n-1)return ps.t[x][a1][a2][a3][a4][a5].n=a1*pl[0][2]+a2*pl[1][2]+a3*pl[2][2]+a4*pl[3][2]+a5*pl[4][2];
							else return ps.t[x][a1][a2][a3][a4][a5].n=fina(x+1, a1, a2, a3, a4, a5, ps);
						}
					}
				}
				if(x==n-1)return ps.t[x][a1][a2][a3][a4][a5].n=fina(x, c[0], c[1], c[2], c[3], c[4], ps)+ps.plan[x].cost;
				return ps.t[x][a1][a2][a3][a4][a5].n=min(fina(x+1, a1, a2, a3, a4, a5, ps),fina(x, c[0], c[1], c[2], c[3], c[4], ps)+ps.plan[x].cost);
			}
		}
	}

	private static int min(int x, int y) {
		// TODO Auto-generated method stub
		return x<y?x:y;
	}

	class Node{
		int n;
	}

	class plan{
		int p[][],cost,n;
	}
}


原文地址:https://www.cnblogs.com/a1439775520/p/12948331.html