QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#244473 | #4479. Slipper | ucup-team203# | AC ✓ | 1071ms | 151500kb | C++20 | 2.4kb | 2023-11-09 09:42:45 | 2023-11-09 09:42:45 |
Judging History
answer
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,int> PIi;
const int maxn=1e6+10;
const ll inf = 0x3f3f3f3f3f3f3f3f;
//9:24 大概
int n,k,p;
vector<Pii> G[maxn];
vector<int> D[maxn];
int maxdep=0;
int dep[maxn];
void Dfs(int x,int f){
dep[x]=dep[f]+1;
D[dep[x]].pb(x);
if(dep[x]>maxdep) maxdep=dep[x];
for(auto& p:G[x]){
int y=p.first;
if(y==f) continue;
Dfs(y,x);
}
}
ll dis[maxn];
ll disLayer[maxn];
priority_queue<PIi,vector<PIi>,greater<PIi>> q;
void Dijkstra(int s){
rep(i,1,n) dis[i]=inf;
rep(i,1,maxdep) disLayer[i]=inf;
dis[s]=0;
q.push({0,s});
while(!q.empty()){
int x=q.top().second; ll d=q.top().first; q.pop();
if(d>dis[x]) continue;
for(auto& p:G[x]){
int y=p.first,w=p.second;
if(dis[y]>d+w){
dis[y]=d+w;
q.push({d+w,y});
}
}
if(dep[x]>k){
int tar=dep[x]-k;
if(disLayer[tar]>d+p){
disLayer[tar]=d+p;
for(auto& e:D[tar]){
if(dis[e]>disLayer[tar]){
dis[e]=disLayer[tar];
q.push({dis[e],e});
}
}
}
}
if(dep[x]+k<=maxdep){
int tar=dep[x]+k;
if(disLayer[tar]>d+p){
disLayer[tar]=d+p;
for(auto& e:D[tar]){
if(dis[e]>disLayer[tar]){
dis[e]=disLayer[tar];
q.push({dis[e],e});
}
}
}
}
}
}
int main()
{
int _; cin>>_;
while(_--){
maxdep=0;
cin>>n;
rep(i,1,n) G[i].clear(),D[i].clear();
rep(i,1,n-1){
int x,y,w; scanf("%d%d%d",&x,&y,&w);
G[x].pb({y,w}),G[y].pb({x,w});
}
dep[0]=0;
Dfs(1,0);
cin>>k>>p;
int s,t; cin>>s>>t;
Dijkstra(s);
printf("%lld\n",dis[t]);
}
fflush(stdin);
getchar();
}
/*
6
6 1 2
3 5 2
2 4 6
5 2 2
5 6 20
3 8
6 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1071ms
memory: 151500kb
input:
5 121753 103252 40559 325002 32674 51809 946614 18343 12099 625962 27677 48601 114048 11146 12478 906161 121147 77390 208299 39512 95642 154696 90603 43508 378490 4829 7818 191754 73699 31412 536840 106916 89894 374802 113739 90049 411062 113123 73246 740213 38047 120942 903325 51907 41500 822541 90...
output:
114128108 55207815 76620494 17377950755 67601
result:
ok 5 lines