結果

問題 No.1037 exhausted
ユーザー ace_amuroace_amuro
提出日時 2024-04-12 12:38:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,651 bytes
コンパイル時間 1,039 ms
コンパイル使用メモリ 83,944 KB
実行使用メモリ 35,212 KB
最終ジャッジ日時 2024-04-12 12:39:00
合計ジャッジ時間 2,267 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
35,180 KB
testcase_01 AC 10 ms
35,092 KB
testcase_02 AC 9 ms
34,980 KB
testcase_03 AC 9 ms
35,156 KB
testcase_04 AC 10 ms
35,016 KB
testcase_05 AC 10 ms
35,036 KB
testcase_06 AC 10 ms
35,124 KB
testcase_07 AC 9 ms
35,160 KB
testcase_08 AC 8 ms
35,192 KB
testcase_09 AC 8 ms
35,000 KB
testcase_10 AC 10 ms
35,120 KB
testcase_11 AC 11 ms
35,064 KB
testcase_12 AC 20 ms
35,072 KB
testcase_13 AC 21 ms
35,156 KB
testcase_14 AC 20 ms
35,096 KB
testcase_15 AC 19 ms
35,152 KB
testcase_16 AC 19 ms
35,212 KB
testcase_17 AC 20 ms
35,048 KB
testcase_18 AC 25 ms
35,172 KB
testcase_19 AC 21 ms
35,100 KB
testcase_20 AC 28 ms
35,148 KB
testcase_21 AC 18 ms
35,064 KB
testcase_22 AC 18 ms
35,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<ctime>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long LL;
const int MR=2e3+10;
int n,V,L;
int x[MR],v[MR],w[MR];
LL dp[MR][MR];
//dp[i][j]是从加油站i出发时,油量恰好等于j时的最少花费
int main(){
    cin>>n>>V>>L;
    for(int i=1;i<=n;i++){
        cin>>x[i]>>v[i]>>w[i];
    }
    memset(dp,0x3f,sizeof(dp));
    dp[0][V]=0;
    for(int i=1;i<=n;i++){
        int t=x[i]-x[i-1];//先计算从上个加油站过来需要多少油
        for(int j=0;j<V;j++){
            //不加油直接出发,说明从上个加油站出发时有j+t升油
            if(j+t<=V) dp[i][j]=min(dp[i][j],dp[i-1][j+t]);
            //加油后出发,那么到加油站i时有j-v[i]升,说明从上个加油站出发时有j-v[i]+t升油
            if(0<=j-v[i] && j-v[i]+t<=V){
                dp[i][j]=min(dp[i][j],dp[i-1][j-v[i]+t]+w[i]);
            }
        }
        //但是如果加油站i出发时,油箱是满的
        //有可能两个加油站坐标一样,且没加油
        if(t==0) dp[i][V]=min(dp[i][V],dp[i-1][V]);
        //或者油箱加满了油,那么到达加油站i时的油量在 V-v[i] 到 V 都有可能
        for(int k=max(0,V-v[i]);k<=V;k++){
            if(k+t<=V) dp[i][V]=min(dp[i][V],dp[i-1][k+t]+w[i]);
        }
    }
    LL ans=1e18;
    //最后从第n站到终点需要L-x[n]升
    for(int j=L-x[n];j<=V;j++) {
        ans=min(ans,dp[n][j]);
    }
    if(ans<1e18) cout<<ans<<endl;
    else cout<<-1<<endl;
    return 0;
}
0