[ H A O I 2010 ] 订 货 [HAOI2010]订货 [HAOI2010]订货
Description:
- 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初的库存量为零,第n月月底的库存量也为零,问如何安排这n个月订购计划,才能使成本最低?每月月初订购,订购后产品立即到货,进库并供应市场,于当月被售掉则不必付存贮费。假设仓库容量为S。
Input Format:
- 第1行:n, m, S (0<=n<=50, 0<=m<=10, 0<=S<=10000)
第2行:U1 , U2 , … , Ui , … , Un (0<=Ui<=10000)
第3行:d1 , d2 , …, di , … , dn (0<=di<=100)
Output Format:
- 只有1行,一个整数,代表最低成本
Sample Input:
- 3 1 1000
2 4 8
1 2 4
Sample Output:
- 34
TJ:
两种解题思路,贪心或者费用流。
费用流思维难度比较低,建图跑就好了
说一下建图
- S点向所有i点连边,容量INF,费用为P[i]
- 所有点向T连边,容量为U[i],费用为0
- 所有i from 1 to n-1 往 i+1 连边,容量为s,费用为m
#include<bits/stdc++.h>
using namespace std;
#define S 0
#define T n+1
const int MAXN = 55;
const int INF = 0x3f3f3f3f;
typedef pair<int,int> pii;
int cargo[MAXN],price[MAXN],n,m,s,pre[MAXN],pree[MAXN],flow[MAXN],dist[MAXN];
struct EDGE{
int to,cap,rev,fee;
EDGE(){}
EDGE(int to, int cap, int rev, int fee){
this->to = to;
this->cap = cap;
this->rev = rev;
this->fee = fee;
}
};
vector<EDGE> G[MAXN];
void ADD_EDGE(int u,int v,int cap,int fee){
G[u].push_back(EDGE(v,cap,(int)G[v].size(),fee));
G[v].push_back(EDGE(u,0,(int)G[u].size()-1,-fee));
}
void build(){
for(int i = 1; i <= n; i++){
ADD_EDGE(S,i,INF,price[i]);
if(i!=n) ADD_EDGE(i,i+1,s,m);
ADD_EDGE(i,T,cargo[i],0);
}
}
bool Dijkstra(){
pre[T] = 0;
flow[S] = INF;
memset(dist,INF,sizeof(dist));
dist[S] = 0;
priority_queue<pii,vector<pii>,greater<pii> > que;
que.push((pii){dist[S],S});
while(!que.empty()){
pii ft = que.top();
que.pop();
int u = ft.second;
int d = ft.first;
if(dist[u]!=d) continue;
for(int i = 0; i < (int)G[u].size(); i++){
EDGE e = G[u][i];
if(!e.cap||dist[u]+e.fee>=dist[e.to]) continue;
dist[e.to] = dist[u] + e.fee;
pre[e.to] = u;
pree[e.to] = i;
flow[e.to] = min(flow[u],e.cap);
que.push((pii){dist[e.to],e.to});
}
}
return pre[T]!=0;
}
int Mincost_Maxflow(){
int cost = 0;
build();
while(Dijkstra()){
cost += dist[T]*flow[T];
int rt = T;
while(rt!=S){
int p = pre[rt];
int l = pree[rt];
G[p][l].cap-=flow[T];
G[rt][G[p][l].rev].cap+=flow[T];
rt = p;
}
}
return cost;
}
int main(){
scanf("%d %d %d",&n,&m,&s);
for(int i = 1; i <= n; i++) scanf("%d",&cargo[i]);
for(int i = 1; i <= n; i++) scanf("%d",&price[i]);
printf("%d\n",Mincost_Maxflow());
return 0;
}
贪心:
每个位置开始往后找,判断之后这个点的花费是否比当前点高
费用为p[i]+(j-i)*m和**p[j]**比较,如果费用更低的话就拿,注意有上限,判断一下。
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 55;
const int MAXS = 1e4+7;
int n,m,s,A[MAXN],B[MAXN],sum[MAXN],tot_money,tot_instore;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> s;
for(int i = 1; i <= n; i++) cin >> A[i];
for(int i = 1; i <= n; i++) cin >> B[i];
for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + A[i];
for(int i = 1; i <= n; i++){
if(tot_instore<A[i]){
tot_money += (A[i]-tot_instore) * B[i];
tot_instore = 0;
}
else tot_instore -= A[i];
for(int j = i+1; j <= n; j++){
if(B[i]+(j-i)*m>=B[j]) break;
if(sum[j]-sum[i]<=tot_instore) continue;
int need = min(sum[j]-sum[i]-tot_instore,A[j]);
if(tot_instore+need>=s){
tot_money += (s-tot_instore)*(B[i]+(j-i)*m);
tot_instore = s;
}
else{
tot_money += need*(B[i]+(j-i)*m);
tot_instore += need;
}
}
}
cout << tot_money << endl;
return 0;
}