QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#391553 | #2161. The Cost of Speed Limits | RobeZH | ML | 0ms | 3624kb | C++23 | 1.8kb | 2024-04-16 17:10:03 | 2024-04-16 17:10:04 |
Judging History
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));
func<void(int,int,int)>dfs=[&](int x,int fa,int lalen){
// cerr<<x<<endl;
vector<ll>dp2(n-1);
if(x!=0){
FR(i,n-1)dp[x][i]=(len[i]<lalen?inf:len[i]-lalen);
FR(i,n-1)dp2[i]=dp[x][i]+c;
}else{
FR(i,n-1)dp[x][i]=dp2[i]=0;
}
for(auto[y,dis]:g[x])if(y!=fa){
dfs(y,x,dis);
ll mi=R::min(dp[y]);
FR(i,n-1)dp[x][i]=min((ll)inf,(ll)dp[x][i]+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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
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: 3624kb
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: 3608kb
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: 3596kb
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: -100
Memory Limit Exceeded
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