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 ...
随机推荐
- tensorflow零起点快速入门(3)
创造并运行数据 创造了-3到3的32条数据,然后通过sess.run获取并显示输出数据. x=tf.linspace(-3.0,3.0,32) print(x) sess=tf.Session() r ...
- 关于CPU的一些操作(CPU设置超频)
常见的几种CPU模式: .ondemand:系统默认的超频模式,按需调节,内核提供的功能,不是很强大,但有效实现了动态频率调节,平时以低速方式运行,当系统负载提高时候自动提高频率.以这种模式运行不会因 ...
- 第一次碰到%*s这个鬼东西。。
printf("%*s",5,"123"); 输出为 ##123 (其中##表示空格) 这个鬼东西是用来控制格式的. 当然也可以用来输出空格个数
- Fonour.AspnetCore 生成SQL SERVER数据库
Install-Package EntityFramework Add-Migration InitialCreate Update-Database
- 【原创】大叔经验分享(73)scala akka actor
import java.util.concurrent.{ExecutorService, Executors, TimeUnit} import akka.actor.{Actor, ActorSy ...
- java启动server时报端口无效解决方法
今天在Java里配置Tomcat服务器,启动时出现如下图报错信息 The server cannot be started because one or more of the ports are i ...
- docker之常见错误
1. docker run -d --name showdoc -p 4999:80 -v /showdoc_data/html:/var/www/html/ star7th/showdoc WARN ...
- string类的总结
一.string类头文件:#include <string>;using namespace std; 二.string类方法: 1.获取string的字符串长度:size(),size返 ...
- Linux工具之watch
watch watch 监测一个命令的运行结果 -n 指定间隔的时间 -d watch会高亮显示变化的区域. -t 会关闭watch命令在顶部的时间间隔, ...
- QTP(16)
一.QTP项目(ECShop) 1.ECShop是一个开源免费的一个B2C的电子商务系统,主要用于商家和顾客进行商品交易操作. 2.ECShop分为前台和后台两个子系统: (1)ECShop前台:顾客 ...