QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#51508 | #4479. Slipper | Winner | AC ✓ | 1703ms | 198592kb | C++11 | 2.4kb | 2022-10-02 15:45:47 | 2022-10-02 15:45:49 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, int> PLI;
typedef pair<int, ll> PIL;
#define x first
#define y second
const int N = 2e6 + 20;
const int mod = 998244353;
const int INF = 0x3f3f3f3f;
int n, k, p, s, t, maxd, depth[N];
vector<PII> node[N];
ll dis[N];
bool vis[N];
struct NODE{
ll dis;
int id, d;
bool operator > (const NODE &x) const{
return dis > x.dis;
}
};
void init(){
maxd = 0;
for(int i = 1; i <= 2 * n; i++){
node[i].clear();
}
}
void dfs(int u, int fa, int d){
depth[u] = d;
for(auto [v, w]: node[u]){
if(v == fa) continue;
dfs(v, u, d + 1);
}
node[n + d].push_back({u, 0});
node[u].push_back({n + d, 0});
maxd = max(maxd, n + d);
}
void dijkstra(){
memset(dis, 0x3f, sizeof dis);
memset(vis, 0, sizeof vis);
priority_queue<NODE, vector<NODE>, greater<NODE> > q;
dis[s] = 0;
q.push({0, s, -1});
while(!q.empty()){
auto it = q.top();
q.pop();
ll nowdis = it.dis;
int nowid = it.id, nowd = it.d;
if(vis[nowid]) continue;
vis[nowid] = true;
for(auto [v, w]: node[nowid]){
if(vis[v]) continue;
if(nowid > n && nowd == depth[v]){
// cout << "!! : " << nowid << " " << v << " " << depth[v] << endl;
continue;
}
if(dis[nowid] + w < dis[v]){
dis[v] = min(dis[v], dis[nowid] + w);
q.push({dis[v], v, depth[nowid]});
// cout << nowid << " " << v << " " << depth[nowid] << " " << depth[v] << endl;
}
}
}
}
void solve(){
scanf("%d", &n);
init();
for(int i = 1; i < n; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
node[u].push_back({v, w});
node[v].push_back({u, w});
}
dfs(1, -1, 1);
scanf("%d%d", &k, &p);
for(int i = 1; i <= n; i++){
if(n + i + k > maxd)
break;
node[n + i].push_back({n + i + k, p});
node[n + i + k].push_back({n + i, p});
}
for(int i = n + 1; i <= maxd; i++){
depth[i] = i - n;
}
scanf("%d%d", &s, &t);
dijkstra();
printf("%lld\n", dis[t]);
}
int main(){
int T = 1;
scanf("%d", &T);
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1703ms
memory: 198592kb
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