Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task :
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 03 May 2023 [18:53]
- */
- #include<bits/stdc++.h>
- using namespace std;
- int dp[100010]; // dp[i] = minimum distance from i to hospital node in subtree
- vector<pair<int ,int > > g[100010];
- pair<int ,int > p[100010];
- void dfs(int now,int par){
- for(auto x:g[now]){
- if(x.first == par) continue;
- p[x.first] = {now,x.second};
- dfs(x.first, now);
- }
- }
- int main(){
- int n,m,s,q;
- cin >> n >> m >> s >> q;
- for(int i=1;i<n;i++){
- int u,v,w;
- cin >> u >> v >> w;
- g[u].push_back({v,w});
- g[v].push_back({u,w});
- }
- dfs(s,0);
- for(int i=1;i<=n;i++)
- dp[i] = 1e9;
- dp[s] = 0;
- while(q--){
- int opr,u;
- cin >> opr >> u;
- if(opr == 0){
- dp[u] = 0;
- int pathWeightFromStart = 0;
- while(u != s){
- dp[u] = min(dp[u],pathWeightFromStart);
- pathWeightFromStart += p[u].second;
- u = p[u].first;
- }
- }else{
- int ans = dp[u],pathWeightFromStart = 0;
- while(u != s){
- ans = min(ans,pathWeightFromStart + dp[u]);
- pathWeightFromStart+=p[u].second;
- u = p[u].first;
- }
- cout << ans << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment