题库链接:https://ac.nowcoder.com/acm/contest/370/F
code:
#include<bits/stdc++.h>
using namespace std;
int n,m,s;
struct edge
{
int to;int cost;
};
vector<edge> e[110005];
bool vis[100005];
int mem[100005];
long long dp(int i,int pre)
{
if(e[i].size()==1&&i!=s)
{
return e[i][0].cost;
}
if(mem[i]!=-1)return mem[i];
vis[i]=1;
long long res=0;
int nxt;int co;
edge tmp;
for(int j=0;j<e[i].size();j++)
{
tmp=e[i][j];nxt=tmp.to;if(vis[nxt])continue;
co=tmp.cost;
res+=dp(nxt,min(co,pre));
}
res=min(res,(long long)pre);
mem[i]=res;
return res;
}
int main()
{
//freopen("in.txt","r",stdin);
memset(mem,-1,sizeof(mem));
scanf("%d%d%d",&n,&m,&s);
int u,v,w;
edge tmp;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
tmp.cost=w;tmp.to=v;
e[u].push_back(tmp);
tmp.to=u;
e[v].push_back(tmp);
}
long long ans=dp(s,0x3f3f3f3f);
cout<<ans<<endl;
return 0;
}