Skip to content

Instantly share code, notes, and snippets.

@enzerr
Created May 17, 2023 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enzerr/04da81ca6d3a7ddc1843d3b2aefef285 to your computer and use it in GitHub Desktop.
Save enzerr/04da81ca6d3a7ddc1843d3b2aefef285 to your computer and use it in GitHub Desktop.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 5;
int dist[maxn];
vector<pair<int, int>> adj[maxn];
void dfs(int x, int pai){
for(auto[v, w] : adj[x]) if(v!=pai)
dist[v] = dist[x]+w, dfs(v, x);
}
int main(){
int n, ini, fim; cin >> n >> ini >> fim;
for(int i = 0; i < n-1; i++){
int a, b, c; cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
dfs(ini, ini);
cout << dist[fim] << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment