1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include<iostream> #include<cmath> #include<cstdio> #include<cstring> #include<queue> #include<stack> #include<vector> #include<set> #include<map> #include<algorithm> #include<cstdlib> #include<ctime>
typedef long long ll;
using namespace std;
namespace mstd{
inline void qread(int &x) { x=0; int f=1; static char c=getchar(); while(!isdigit(c)) {if(c=='-') f=-f; c=getchar();} while(isdigit(c)) x=(x*10+c-'0'),c=getchar(); x*=f; }
inline void qread(long long &x) { x=0; long long f=1; static char c=getchar(); while(!isdigit(c)) {if(c=='-') f=-f; c=getchar();} while(isdigit(c)) x=(x*10+c-'0'),c=getchar(); x*=f; } }
const int maxn=100100;
int n,head[maxn],size,f[maxn],rt;
struct edge{ int next,to,dis; }e[maxn];
inline void addedge(int next,int to,int dis) { e[++size].to=to; e[size].dis=dis; e[size].next=head[next]; head[next]=size; }
void dfs(int t,int fat) { int i,j,k; int tmp=0; for(i=head[t];i;i=e[i].next) { j=e[i].to; k=e[i].dis; if(j==fat) continue; f[j]=k; dfs(j,t); tmp+=f[j]; } if(tmp) f[t]=min(tmp,f[t]); }
int main() { int i,j; memset(f,0x3f,sizeof(f)); mstd::qread(n); mstd::qread(rt); int t1,t2,t3; for(i=1;i<n;i++) mstd::qread(t1),mstd::qread(t2),mstd::qread(t3),addedge(t1,t2,t3),addedge(t2,t1,t3); dfs(rt,rt); printf("%d\n",f[rt]); return 0; }
|