牛客网练习赛27(水图)

3 篇文章 0 订阅
2 篇文章 0 订阅

题目链接:https://www.nowcoder.com/acm/contest/188/C

解题思路:要遍历所有的结点,我们可以按经过的次数把点分成两类:经过一次,经过两次。那么我们可以把所有边的权值加起来然后乘以二,减去从起点出发经过一次的边的权值的总和的最大值就是所求的答案了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50000+10;
struct edg{
	int to,cost;
}tem;
vector<edg> node[maxn];
ll dfs(int x,int y){//x表示从点x出发,y表示x是从y到达的
	ll mx=0;
	//cout<<x<<endl;
	for(int i=0;i<node[x].size();i++)
	{
		ll sum=0;
		int j=node[x][i].to;
		if(j!=y)//避免往回走
		{
			sum+=node[x][i].cost;
			sum+=dfs(j,x);
		}
		mx=max(sum,mx);
	}
	return mx;
}
int main()
{
	int n,x,i;
	int st,en,wi;
	ll sum=0,mx;
	cin>>n>>x;
	for(i=1;i<n;i++)
	{
		cin>>st>>en>>wi;
		sum+=2*wi;
		tem.to=en;
		tem.cost=wi;
		node[st].push_back(tem);
		tem.to=st;
		node[en].push_back(tem);
	}
	mx=dfs(x,x);
	printf("%lld\n",sum-mx);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值