標籤:int class fread queue algorithm dea stream while cstring
某地區有m座礦,其中第i號礦每年產量為ai噸,現有火力發電站一個,每年需用煤b噸,每年啟動並執行固定費用(包括折舊費,不包括煤的運費)為h元,每噸原煤從第i號礦運到原有發電廠的運費為Ci0(i=1,2,…,m;j=1,2,…,n)。
現規劃建立一個發電廠,m座煤礦每年開採的原煤將全部供給這兩座發電廠。現有n個備選的廠址。若在第j號備選廠址建新廠,每年啟動並執行固定費用為hi元。每噸原煤從第i號礦運到j號備選廠址的運費為Cij(i=1,2,…,m;j=1,2,…,n)。
試問:應把新廠廠址選取在何處?m座煤礦開採的原煤應如何分配給兩個發電廠,才能使每年的總費用(發電廠運行費用與原煤費用之和)為最小。
題解:貪心;
這題比較直接,只讓求使費用最小,沒有收益這一項限制,所以一維的貪心就夠了;
主要操作:排序;
提醒:答案可能超出int範圍,以及注意數組範圍;
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<ctime>#include<algorithm>#include<cstdlib>#include<map>#include<set>#include<vector>#include<queue>#include<cmath>using namespace std;#define FILE "dealing"#define LL long long #define up(i,j,n) for(int i=j;i<=n;i++)#define pii pair<LL,LL>#define piii pair<LL,pair<LL,LL> >template<typename T> inline bool chkmin(T &a,T b){return a>b?a=b,true:false;}template<typename T> inline bool chkmax(T &a,T b){return a<b?a=b,true:false;}namespace IO{ char *fs,*ft,buf[1<<15]; inline char gc(){return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;} inline int read(){ LL x=0;int ch=gc();bool f=0; while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=1;ch=gc();} while(ch<=‘9‘&&ch>=‘0‘){x=(x<<1)+(x<<3)+ch-‘0‘;ch=gc();} return f?-x:x; }}using namespace IO;namespace OI{ const LL maxn(50100),inf(100000000000LL),mod(10000); int M,B,H,N; int a[maxn],h[maxn],c[55][maxn]; struct node{ int id,d; inline bool operator<(const node& b)const{return d<b.d;} }e[maxn]; void slove(){ M=read(),B=read(),H=read(),N=read(); up(i,1,M)a[i]=read(); up(i,1,N)h[i]=read(); up(i,0,N)up(j,1,M)c[i][j]=read();//由j廠運到i位置 LL Min=inf,Id; up(i,1,N){ up(j,1,M)e[j].id=j,e[j].d=c[0][j]-c[i][j]; sort(e+1,e+M+1); LL sum=0,t=0; up(j,1,M){ if(sum<B){ sum+=a[e[j].id]; t+=c[0][e[j].id]*a[e[j].id]; if(sum>B){ t=t-(sum-B)*c[0][e[j].id]+(sum-B)*c[i][e[j].id]; sum=B; } } else t+=c[i][e[j].id]*a[e[j].id]; } t+=H+h[i]; if(t<Min)Min=t,Id=i; } printf("%I64d\n%I64d\n",Id,Min); }}int main(){ freopen(FILE".in","r",stdin); freopen(FILE".out","w",stdout); using namespace OI; slove(); return 0;}
[haoi2010]工廠選址