QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#385165 | #2161. The Cost of Speed Limits | bulijiojiodibuliduo# | WA | 2ms | 11580kb | C++17 | 2.0kb | 2024-04-10 16:11:40 | 2024-04-10 16:11:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=101000;
int n,m,c,sz[N];
vector<PII> e[N],ch[N];
ll dp[N],pd[25][N];
VI wt;
void dfs1(int u,int f) {
sz[u]=1;
for (auto [v,w]:e[u]) if (v!=f) {
dfs1(v,u);
ch[u].pb(mp(v,w));
sz[u]+=sz[v];
}
sort(all(ch[u]),[&](PII a,PII b) {
return sz[a.fi]>sz[b.fi];
});
}
const ll inf=1ll<<50;
void dfs2(int u,int dep) {
//printf("?????????????? %d %d\n",u,dep);
rep(j,0,SZ(ch[u])) {
int v=ch[u][j].fi;
int w=ch[u][j].se;
if (j==0) {
dfs2(v,dep);
dp[u]+=dp[v];
rep(k,0,m) pd[dep][k]=min(pd[dep][k],dp[v])+(wt[w]<=wt[k]?wt[k]-wt[w]:inf);
} else {
memset(pd[dep+1],0,m*sizeof(int));
dfs2(v,dep+1);
dp[u]+=dp[v];
rep(k,0,m) pd[dep][k]+=min(pd[dep+1][k],dp[v])+(wt[w]<=wt[k]?wt[k]-wt[w]:inf);
}
}
dp[u]+=(SZ(e[u])>1?SZ(e[u]):0)*c;
//printf("%d : %lld\n",u,dp[u]);
//rep(k,0,m) printf("%lld ",pd[dep][k]); puts("");
}
int main() {
scanf("%d%d",&n,&c);
rep(i,1,n) {
int u,v,s;
scanf("%d%d%d",&u,&v,&s);
e[u].pb(mp(v,s));
e[v].pb(mp(u,s));
wt.pb(s);
}
sort(all(wt)); wt.erase(unique(all(wt)),wt.end());
m=SZ(wt);
rep(u,1,n+1) {
for (auto &[v,w]:e[u]) w=lower_bound(all(wt),w)-wt.begin();
}
dfs1(1,0);
dfs2(1,0);
ll ans=dp[1];
rep(k,0,m) ans=min(ans,pd[0][k]);
printf("%lld\n",ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 11580kb
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:
273
result:
wrong answer 1st lines differ - expected: '272', found: '273'