mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-08-21 18:52:07 +00:00
重构算法实现,移除未使用的 DFS 函数,优化 DP 函数逻辑,并更新输入输出处理
This commit is contained in:
parent
7a20684212
commit
73cea4d52e
@ -1,42 +1,34 @@
|
|||||||
/*
|
|
||||||
2 10 7 11
|
|
||||||
2 10
|
|
||||||
6 1
|
|
||||||
|
|
||||||
n=2, W=10, B=7, X=11
|
|
||||||
c[1]=2,c[2]=10
|
|
||||||
cost[1]=6,cost[2]=1
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
#include <limits>
|
||||||
|
#include <ostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using ll = int64_t;
|
using ll = int64_t;
|
||||||
|
|
||||||
#define il static inline
|
ll n,w,b,x;
|
||||||
ll n,w,b,x,ans=0;
|
|
||||||
std::vector<ll> c,cost;
|
std::vector<ll> c,cost;
|
||||||
|
constexpr ll inf = std::numeric_limits<ll>::min();
|
||||||
|
|
||||||
il void dfs(ll nn,ll nw,ll nb,ll nc){
|
static inline ll dp(ll i,ll j){
|
||||||
if(nn>n){
|
if(i==0){
|
||||||
ans=std::max(ans,nc);
|
if(j==0)return w;
|
||||||
return;
|
else return inf;
|
||||||
}
|
}
|
||||||
for(ll i=0;i<=c[nn];i++){
|
static std::vector<std::vector<ll>> vdp(n+1,std::vector<ll>(10000+1,inf));
|
||||||
if(nw<cost[nn]*i)return;
|
if(vdp[i][j]!=inf)return vdp[i][j];
|
||||||
dfs(nn+1, std::min(nw-cost[nn]*i+x,nb+b*i), nb+b*i, nc+i);
|
for(ll k=0;k<=c[i];k++){
|
||||||
|
if(j-k<0||dp(i-1,j-k)<0)continue;
|
||||||
|
vdp[i][j]=std::max(vdp[i][j],std::min(dp(i-1, j-k)+x,w+(j-k)*b)-cost[i]*k);
|
||||||
}
|
}
|
||||||
|
return vdp[i][j];
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
std::iostream::sync_with_stdio(false);
|
std::iostream::sync_with_stdio(false);
|
||||||
std::cin.tie(nullptr);
|
std::cin.tie(nullptr);
|
||||||
std::cout.tie(nullptr);
|
|
||||||
|
|
||||||
std::cin>>n>>w>>b>>x;
|
std::cin>>n>>w>>b>>x;
|
||||||
c.resize(n+1);
|
c.resize(n+1);
|
||||||
cost.resize(n+1);
|
cost.resize(n+1);
|
||||||
@ -46,6 +38,11 @@ int main(){
|
|||||||
for(ll i=1;i<=n;i++){
|
for(ll i=1;i<=n;i++){
|
||||||
std::cin>>cost[i];
|
std::cin>>cost[i];
|
||||||
}
|
}
|
||||||
dfs(1, w, b, 0);
|
for(ll i=1000;i>=0;i--){
|
||||||
std::cout<<ans<<'\n';
|
if(dp(n, i)>=0){
|
||||||
}
|
std::cout<<i<<std::endl;
|
||||||
|
_Exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user