Description
一个工厂要供应顾客 k k 个月的需求,第个月工厂买入原料的代价为 ci c i ,一份材料可以生产一台电脑,工本费为 mi m i ,产量至多为 pi p i ,而顾客需求量为 di d i ,第 i i 个月生产出的电脑和买的原料可以存储起来,存储一台电脑一个月的租金为,储存一份原料一个月的租金为 Ri R i ,仓库可以储存无限的材料,但是至多只能储存 ei e i 电脑到下一个月,问满足顾客所有需求的前提下所需要的最小代价
Input
第一行一整数 T T 表示用例组数,每组用例首先输入一整数表示要满足需求的月数,之后 k k 行每行输入四个整数,最后 k−1 k − 1 行每行输入三个整数 ei,Ri,Ei e i , R i , E i
(1≤T≤200,2≤k≤50000,0≤ci,di,mi,pi≤104,0≤ei≤108,0≤Ri,Ei≤104) ( 1 ≤ T ≤ 200 , 2 ≤ k ≤ 50000 , 0 ≤ c i , d i , m i , p i ≤ 10 4 , 0 ≤ e i ≤ 10 8 , 0 ≤ R i , E i ≤ 10 4 )
Output
输出最小代价,如果无法满足顾客需求则输出 −1 − 1
Sample Input
2
2
10 5 3 6
15 7 2 8
2 3 2
2
0 8 0 7
0 0 0 0
0 0 0
Sample Output
170
-1
Solution
由于原料不限购且可以无限存储,故每次必然贪心的选取价格最低的原料,即第 i i 天的原料价格为,对于电脑,可以先把每天可以生产的量都生产出来,每次选取其中成本最低的电脑出售,而当仓库容量有限制时就将成本最贵的电脑丢弃,由于存储电脑仓库每天的租金不同,可以首先让第 i i 天成产的电脑价成本为,即先给该电脑交前 i−1 i − 1 天的租金,第 i i 天卖电脑时再给补上的租金即可,可以用 map m a p 维护每种成本电脑的数量,注意首先要提前判断产量是否能够跟得上需求,如果跟不上需求直接输出 −1 − 1 即可
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
namespace fastIO
{
#define BUF_SIZE 100000
//fread -> read
bool IOerror=0;
inline char nc()
{
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend)
{
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1)
{
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch)
{
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
inline void read(int &x)
{
char ch;
while(blank(ch=nc()));
if(IOerror)return;
int sgn=1;
if(ch=='-')sgn=-1;
for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
x=sgn*x;
}
#undef BUF_SIZE
};
using namespace fastIO;
typedef long long ll;
typedef pair<int,int>P;
#define maxn 50005
int T,k,c[maxn],d[maxn],m[maxn],p[maxn],e[maxn],R[maxn],E[maxn];
map<int,int>M;
map<int,int>::iterator it;
int main()
{
read(T);
while(T--)
{
read(k);
for(int i=1;i<=k;i++)
{
read(c[i]);read(d[i]);read(m[i]);read(p[i]);
}
for(int i=1;i<k;i++)
{
read(e[i]);read(R[i]);read(E[i]);
E[i]+=E[i-1];
}
int flag=1;
int res=0;
for(int i=1;i<=k;i++)
{
res+=p[i];
if(res<d[i])
{
flag=0;
break;
}
res-=d[i];
res=min(res,e[i]);
}
if(!flag)
{
printf("-1\n");
continue;
}
for(int i=2;i<=k;i++)c[i]=min(c[i],c[i-1]+R[i-1]);
M.clear();
ll ans=0;
int sum=0;
for(int i=1;i<=k;i++)
{
M[m[i]+c[i]-E[i-1]]+=p[i];
sum+=p[i]-d[i];
while(d[i])
{
it=M.begin();
if(it->second<=d[i])
{
ans+=(ll)(it->first+E[i-1])*(it->second);
d[i]-=it->second;
M.erase(it);
}
else
{
ans+=(ll)(it->first+E[i-1])*d[i];
M[it->first]-=d[i];
d[i]=0;
}
}
if(sum<=e[i])continue;
int temp=sum-e[i];
sum=e[i];
while(temp)
{
it=M.end();
it--;
if(it->second<=temp)
{
temp-=it->second;
M.erase(it);
}
else
{
M[it->first]-=temp;
temp=0;
}
}
}
printf("%lld\n",ans);
}
return 0;
}