QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#391583#2161. The Cost of Speed LimitsRobeZHRE 2554ms1571028kbC++231.9kb2024-04-16 17:16:582024-04-16 17:16:58

Judging History

你现在查看的是最新测评结果

  • [2024-04-16 17:16:58]
  • 评测
  • 测评结果:RE
  • 用时:2554ms
  • 内存:1571028kb
  • [2024-04-16 17:16:58]
  • 提交

answer

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
// #include<atcoder/all>
#define FR(i,n) for(int i=0;i<n;++i)
// #define rep(i,n) for(int i=1;i<=n;++i)
#define eb emplace_back
#define st first
#define nd second
#define x1 fxxkcjb
#define y1 fxxkyzc
#define x2 fxxkzzy
#define y2 fxxknitit
using namespace std;
namespace R=ranges;
template<typename T>
using func=function<T>;
template<typename T>
using vec=vector<T>;
using ll=long long;
using ld=long double;
using i128=__int128;
using pii=pair<int,int>;
const int inf=(int)2e9+9;
void sol(){
    int n,c;cin>>n>>c;
    vector g(n,vector<pii>());
    vector<int>len(n-1);
    FR(tim,n-1){
        int u,v,w;cin>>u>>v>>w;--u,--v;
        g[u].eb(v,w);g[v].eb(u,w);
        len[tim]=w;
    }
    vector dp(n,vector<int>(n-1));
    vector<ll>dp2(n-1);
    func<void(int,int,int)>dfs=[&](int x,int fa,int lalen){
        // cerr<<x<<endl;
        if(x!=0){
            FR(i,n-1)dp[x][i]=(len[i]<lalen?inf:len[i]-lalen);
        }else{
            FR(i,n-1)dp[x][i]=0;
        }
        for(auto[y,dis]:g[x])if(y!=fa){
            dfs(y,x,dis);
            FR(i,n-1)dp[x][i]=min((ll)inf,(ll)dp[x][i]+dp[y][i]);
        }
        if(x!=0){
            FR(i,n-1)dp2[i]=(len[i]<lalen?inf:len[i]-lalen)+c;
        }else{
            FR(i,n-1)dp2[i]=0;
        }
        for(auto[y,dis]:g[x])if(y!=fa){
            int mi=dp[y][0];
            FR(i,n-1)mi=min(mi,dp[y][i]);
            FR(i,n-1)dp2[i]=min((ll)inf,(ll)dp2[i]+c+mi);
        }
        FR(i,n-1){
            dp[x][i]=min((ll)dp[x][i],dp2[i]);
            // cerr<<x<<" "<<i<<" "<<dp[x][i]<<endl;
        }
    };
    dfs(0,-1,-1);
    int ans=dp[0][0];
    FR(i,n-1)ans=min(ans,dp[0][i]);
    cout<<ans<<"\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    sol();
    return 0;
}
/*
5 2
1 2 10
1 3 5
1 4 7
2 5 9
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3580kb

input:

13 20
1 8 101
1 9 30
1 2 100
1 3 100
2 4 75
2 5 70
2 6 82
2 7 77
3 10 73
3 11 69
3 12 83
3 13 79

output:

272

result:

ok single line: '272'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3536kb

input:

9 10
1 6 26
2 6 27
3 6 28
4 6 29
5 6 30
7 9 14
8 9 1
9 6 10

output:

60

result:

ok single line: '60'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3888kb

input:

7 64
2 1 194
3 1 187
4 2 158
5 1 42
6 5 101
7 5 80

output:

308

result:

ok single line: '308'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

6 32
2 1 110
3 2 36
4 1 54
5 3 101
6 5 71

output:

178

result:

ok single line: '178'

Test #5:

score: 0
Accepted
time: 2357ms
memory: 1569424kb

input:

20000 100
2273 4097 98627
14155 14055 33506
16060 6363 28081
14840 12695 23379
11520 7892 5831
6633 13834 73944
19218 19341 62064
14392 160 58289
18147 209 46443
16941 5453 55103
11895 12849 31201
10275 1622 71781
19595 6349 14232
19485 10800 9778
10745 13541 44786
18727 15264 25726
5847 12815 43894...

output:

2999800

result:

ok single line: '2999800'

Test #6:

score: 0
Accepted
time: 1128ms
memory: 1571028kb

input:

20000 1000
1 2 99998
2 3 99994
3 4 99992
4 5 99989
5 6 99987
6 7 99983
7 8 99982
8 9 99978
9 10 99973
10 11 99970
11 12 99965
12 13 99961
13 14 99957
14 15 99954
15 16 99949
16 17 99946
17 18 99945
18 19 99940
19 20 99935
20 21 99932
21 22 99927
22 23 99923
23 24 99920
24 25 99919
25 26 99918
26 27 ...

output:

2088429

result:

ok single line: '2088429'

Test #7:

score: 0
Accepted
time: 1069ms
memory: 1571004kb

input:

20000 1000
1 2 4
2 3 6
3 4 9
4 5 13
5 6 15
6 7 17
7 8 22
8 9 23
9 10 27
10 11 28
11 12 31
12 13 36
13 14 38
14 15 39
15 16 44
16 17 46
17 18 49
18 19 52
19 20 55
20 21 60
21 22 62
22 23 64
23 24 67
24 25 68
25 26 71
26 27 72
27 28 76
28 29 77
29 30 82
30 31 86
31 32 89
32 33 90
33 34 95
34 35 96
35 ...

output:

2098509

result:

ok single line: '2098509'

Test #8:

score: 0
Accepted
time: 1163ms
memory: 1568168kb

input:

20000 100000
1 4 1
1 5 1
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 1
1 12 1
1 13 1
1 14 1
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 1
1 21 1
1 22 1
1 23 1
1 24 1
1 25 1
1 26 1
1 27 1
1 28 1
1 29 1
1 30 1
1 31 1
1 32 1
1 33 1
1 34 1
1 35 1
1 36 1
1 37 1
1 38 1
1 39 1
1 40 1
1 41 1
1 42 1
1 43 1
1 44 1
1 45 1...

output:

1999780001

result:

ok single line: '1999780001'

Test #9:

score: 0
Accepted
time: 2554ms
memory: 1570972kb

input:

20000 100
1 2 114
2 3 243
3 4 243
4 5 239
5 6 248
6 7 122
7 8 187
8 9 281
9 10 241
10 11 209
11 12 119
12 13 241
13 14 243
14 15 236
15 16 151
16 17 272
17 18 132
18 19 281
19 20 272
20 21 194
21 22 233
22 23 220
23 24 271
24 25 278
25 26 153
26 27 108
27 28 229
28 29 130
29 30 291
30 31 245
31 32 2...

output:

1636347

result:

ok single line: '1636347'

Test #10:

score: -100
Runtime Error

input:

1 12345

output:


result: