Description
某地区有m座煤矿,其中第i号矿每年产量为ai吨,现有火力发电厂一个,每年需用煤b吨,每年运行的固定费用(包括折旧费,不包括煤的运费)为h元,每吨原煤从第i号矿运到原有发电厂的运费为Ci0(i=1,2,…,m)。
现规划新建一个发电厂,m座煤矿每年开采的原煤将全部供给这两座发电厂。现有n个备选的厂址。若在第j号备选厂址建新厂,每年运行的固定费用为hj元。每吨原煤从第i号矿运到j号备选厂址的运费为Cij(i=1,2,…,m;j=1,2,…,n)。
试问:应把新厂厂址选取在何处?m座煤矿开采的原煤应如何分配给两个发电厂,才能使每年的总费用(发电厂运行费用与原煤运费之和)为最小。
Input
第1行: m b h n
第2行: a1 a2 … am (0<=ai<=500, a1+a2+...+an>=b)
第3行: h1 h2 … hn (0<=hi<=100)
第4行: C10 C20 … Cm0 (0<=Cij<=50)
第5行: C11 C21 … Cm1
… …
第n+4行:C1n C2n … Cmn
Output
第1行:新厂址编号,如果有多个编号满足要求,输出最小的。
第2行:总费用
题解:
暴力枚举选哪个地方建厂,然后贪心选择那几个矿场给第0个工厂提供煤矿。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int N=50100;
int m,b,n,h[N];
int a[N],c[120][N];
struct node{
int x;
ll dis;
}sa[N];
bool cmp(node x,node y)
{
return x.dis<y.dis;
}
ll get(int x)
{
ll ans=0;
for(int i=1;i<=m;i++) ans+=1ll*a[i]*c[x][i];
ans+=h[0]+h[x];
for(int i=1;i<=m;i++)
{
sa[i].x=i;
sa[i].dis=c[0][i]-c[x][i];
}
sort(sa+1,sa+1+m,cmp);
int yu=b;
for(int i=1;i<=m;i++)
{
if(yu>=a[sa[i].x])
{
yu-=a[sa[i].x];
ans+=1ll*a[sa[i].x]*sa[i].dis;
}
else
{
ans+=yu*sa[i].dis;
yu-=yu;
}
if(yu==0) break;
}
return ans;
}
int main()
{
scanf("%d%d%d%d",&m,&b,&h[0],&n);
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&h[i]);
for(int i=0;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&c[i][j]);
}
}
ll ans=214748364700000000ll;
int id;
for(int i=1;i<=n;i++)
{
ll yu=get(i);
if(yu<ans)
{
ans=yu;
id=i;
}
}
printf("%d\n%lld",id,ans);
}