D. Eternal Victory
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal!

He decided to visit all n cities of Persia to find the best available mountain, but after the recent war he was too tired and didn't want to traverse a lot. So he wanted to visit each of these n cities at least once with smallest possible traverse. Persian cities are connected with bidirectional roads. You can go from any city to any other one using these roads and there is a unique path between each two cities.

All cities are numbered 1 to n. Shapur is currently in the city 1 and he wants to visit all other cities with minimum possible traverse. He can finish his travels in any city.

Help Shapur find how much He should travel.

Input

First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.

Next n - 1 lines contain 3 integer numbers each xiyi and wi (1 ≤ xi, yi ≤ n, 0 ≤ wi ≤ 2 × 104). xi and yi are two ends of a road and wi is the length of that road.

Output

A single integer number, the minimal length of Shapur's travel.

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Examples
input
3
1 2 3
2 3 4
output
7
input
3
1 2 3
1 3 3
output
9

算法:dfs + 思维

题意:给你n个点,n - 1条边,问你从点1开始,需要经过所有的点,所走过的最短路径时多少。

题解:如果你要经过所有的点的话,那就势必有n - 2条路你需要走两遍(自己画图理解一下),因为你每次走到一条路的尽头,你就需要返回来,去走另一条路,只有最后一条路只要走一遍。那么,这样的话,你就只需要求出最长的那一条路,然后用 总路径的权值和 * 2 - 最长子路 就是行了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <vector> using namespace std; #define INF 0x3f3f3f3f
typedef long long ll; const int maxn = 1e5+; vector<pair<ll, ll> > g[maxn]; ll cnt;
int vis[maxn]; void dfs(ll u, ll cal) {
int len = g[u].size();
cnt = max(cal, cnt); //找到最长子路径
for(int i = ; i < len; i++) {
ll v = g[u][i].first;
ll w = g[u][i].second;
if(!vis[v]) {
vis[v] = ;
dfs(v, cal + w);
}
}
} int main() {
int n;
scanf("%d", &n);
ll sum = ;
for(int i = ; i < n; i++) {
ll u, v, w;
scanf("%I64d %I64d %I64d", &u, &v, &w);
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
sum += w * ; //求出总路径的权值和 * 2
}
cnt = ;
vis[] = ;
dfs(1LL, 0LL);
printf("%I64d\n", sum - cnt);
return ;
}

D. Eternal Victory(dfs + 思维)的更多相关文章

  1. hdu6035[dfs+思维] 2017多校1

    /*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...

  2. hiboCoder 1041 国庆出游 dfs+思维

    先抽象出一棵以1做为根结点的树.给定了访问序列a[1..m]. 考虑两种特殊情况: 1.访问了某个a[j],但是存在a[i]没有访问且i < j,出现这种情况说明a[j]一定是a[i]的祖先节点 ...

  3. newcoder F石头剪刀布(DFS + 思维)题解

    题意:wzms 今年举办了一场剪刀石头布大赛,bleaves 被选为负责人. 比赛共有 2n 个人参加, 分为 n 轮, 在每轮中,第 1 位选手和第 2 位选手对战,胜者作为新的第 1 位选手, 第 ...

  4. 【CF61D】Eternal Victory

    题目大意:给定一棵 N 个节点的树,求从 1 号节点(根节点)出发,任意节点结束,且至少经过每个节点一次的最短路径是多少. 题解:首先考虑最终要回到根节点的情况,可以发现最短路径长度一定等于该树边权的 ...

  5. Codeforces 931D Peculiar apple-tree(dfs+思维)

    题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...

  6. UVALive - 6436 —(DFS+思维)

    题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...

  7. Divide by three, multiply by two(DFS+思维)

    Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, an ...

  8. HDU 6060 RXD and dividing(dfs 思维)

    RXD and dividing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  9. Making Genome in Berland (DFS+思维)

    个人心得:被这周的专题名坑了,一直用字典树,明明题目看得很清楚了,不存在相同的字母,即每个字母最多只有一个直接后驱,那么只要用DFS走开头就好了, 思想很巧妙,用vector,记录后驱,同时用visi ...

随机推荐

  1. 怎样理解 Vue 中的计算属性 computed 和 methods ?

    需求: 在 Vue 中, 我们可以像下面这样通过在 引号 或 双花括号 内写 js 表达式去做一些简单运算, 这是可以的, 不过这样写是不直观的, 而且在 html 中 夹杂 一些运算逻辑这种做法其实 ...

  2. 区间问题 codeforces 422c+hiho区间求差问

    先给出一个经典的区间处理方法 对每个区间 我们对其起点用绿色标识  终点用蓝色标识 然后把所有的点离散在一个坐标轴上 如下图 这样做有什么意义呢.由于我们的区间可以离散的放在一条轴上面那么我们在枚举区 ...

  3. DPI,像素,英寸的关系

    DPI * 英寸 = 像素 eg:在150dpi打印 (2 x 4)英寸的照片 等到多少像素的图像 (150*2)x (150*4) =300 x 600像素 同理可得  已知像素.英寸大小 求DPI ...

  4. java 计算中位数方法

    最近工作需要 要求把python的代码写成java版本,python中有一个np.median()求中位数的方法,java决定手写一个 先说说什么是中位数: 中位数就是中间的那个数, 如果一个集合是奇 ...

  5. spring注解定时器

    上一篇文章写了一个在配置文件中设置时间的定时器,现在来写一个注解方式的定时器: 1.工程结构如下: 2.需要执行的代码块: package com.Task; import org.springfra ...

  6. md加密 16位 32位

    16位大写 //生成MD5 public static String getMD5(String message) { String md5 = ""; try { Message ...

  7. NativeScript —— 初级入门(跨平台的手机APP应用)《一》

    NativeScript简介 NativeScript是一个相当新的开源开发系统,几乎完全用JavaScript创建跨平台移动应用程序,带有一些可选的CSS和XML来简化显示布局的开发.您可以在htt ...

  8. 【0】Zookeeper Q&A

    1.Observer角色如何配置? Zookeeper集群中的中的Leader和Follower角色是由服务器启动时期的Leader选举产生的,Observer不参与选举,此角色的节点需要在配置文件z ...

  9. linux学习笔记七

    #文件权限很重要,有些时候删除和新建文件没有权限根本操作不了,linux一切皆是文件,所以必须得了解下权限了. 文件的一般权限 简单的ls -ld 命令就能看到权限,dr-xr-x---补全应该是dr ...

  10. main特别之处

    //package new_Object; public class Main{ public static void main(String[] args) { System.out.println ...