D. Eternal Victory(dfs + 思维)
2 seconds
256 megabytes
standard input
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.
First line contains a single natural number n (1 ≤ n ≤ 105) — the amount of cities.
Next n - 1 lines contain 3 integer numbers each xi, yi 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.
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).
3
1 2 3
2 3 4
7
3
1 2 3
1 3 3
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 + 思维)的更多相关文章
- hdu6035[dfs+思维] 2017多校1
/*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...
- hiboCoder 1041 国庆出游 dfs+思维
先抽象出一棵以1做为根结点的树.给定了访问序列a[1..m]. 考虑两种特殊情况: 1.访问了某个a[j],但是存在a[i]没有访问且i < j,出现这种情况说明a[j]一定是a[i]的祖先节点 ...
- newcoder F石头剪刀布(DFS + 思维)题解
题意:wzms 今年举办了一场剪刀石头布大赛,bleaves 被选为负责人. 比赛共有 2n 个人参加, 分为 n 轮, 在每轮中,第 1 位选手和第 2 位选手对战,胜者作为新的第 1 位选手, 第 ...
- 【CF61D】Eternal Victory
题目大意:给定一棵 N 个节点的树,求从 1 号节点(根节点)出发,任意节点结束,且至少经过每个节点一次的最短路径是多少. 题解:首先考虑最终要回到根节点的情况,可以发现最短路径长度一定等于该树边权的 ...
- Codeforces 931D Peculiar apple-tree(dfs+思维)
题目链接:http://codeforces.com/contest/931/problem/D 题目大意:给你一颗树,每个节点都会长苹果,然后每一秒钟,苹果往下滚一个.两个两个会抵消苹果.问最后在根 ...
- UVALive - 6436 —(DFS+思维)
题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...
- 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 ...
- HDU 6060 RXD and dividing(dfs 思维)
RXD and dividing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- Making Genome in Berland (DFS+思维)
个人心得:被这周的专题名坑了,一直用字典树,明明题目看得很清楚了,不存在相同的字母,即每个字母最多只有一个直接后驱,那么只要用DFS走开头就好了, 思想很巧妙,用vector,记录后驱,同时用visi ...
随机推荐
- NHiberante的优缺点
参考 NHiberante的优缺点 3.1 优点 (1).面向对象:NHiberante的使用时只需要操纵对象,使开发更对象化,抛弃了数据库中心的思想,完全的面向对象思想. (2).透明持久化:带有持 ...
- RPC一般指远程过程调用协议
RPC一般指远程过程调用协议 RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...
- C# Reflection exception Method not found
C# Reflection exception Method not found Ok I figured it out. The server has Microsft .NET 4.0 insta ...
- ubuntu下安装python-selenuim自动化测试的谷歌浏览器驱动安装的位置
谷歌插件下载地址 https://npm.taobao.org/mirrors/chromedriver selenium下载地址 https://pypi.org/simple/selenium/ ...
- 高并发之nginx限制
Nginx限速模块分为哪几种?按请求速率限速的burst和nodelay参数是什么意思?漏桶算法和令牌桶算法究竟有什么不同?本文将带你一探究竟. 我们会通过一些简单的示例展示Nginx限速限流模块是如 ...
- js 使用sessionStorage总结与实例
作用:它只是可以将一部分数据在当前会话中保存下来,刷新页面数据依旧存在.但当页面关闭后,sessionStorage 中的数据就会被清空 sessionStorage的方法setItem存储value ...
- 正着打星星(js)
//让用户输入行数,使用for循环嵌套打出正着的星星来,行数等于用户输入的数字 //例如:用户输入6 // * // *** // ***** // ******* // ********* // * ...
- JVM学习(三):垃圾回收算法
局部性原理和分代回收思想 大学学习操作系统或者计算机组成原理的时候都提到一个重要概念,叫局部性原理. 局部性原理是指CPU访问存储器时,无论是存取指令还是存取数据,所访问的存储单元都趋于聚集在一个较小 ...
- qt tableview中如何添加右键菜单且不可编辑单元格
QTableView是一个比较实用的类,下面教给大家如何在QTableView中添加右键菜单. #include <QMenu>#include <QAction> QTabl ...
- 第十章、random模块
目录 第十章.random模块 第十章.random模块 #随机生成0-1之间的小数 import random print(random.random()) print(random.randint ...