旅行商问题【山财新生赛E】
链接:https://ac.nowcoder.com/acm/contest/547/E
来源:牛客网
题目描述
旅行商来到了一个新的国家,这个国家有N个城市,他们直接由N-1条道路相连接,每条道路的长度不尽相同
旅行商现在在1号城市,若他要每一个城市都游览一遍,他需要行走的最短路程是多少?
输入描述:
第一行一个数N (50000>N>1)
代表城市个数
之后N-1行
每行三个数x y z
代表从x到y的距离为z
输出描述:
输出最小距离
示例1
输入
3
1 2 1
1 3 1
输出
3
思路 : N个城市,N-1条道路,如果要走遍所有的城市,只要有一个城市与>1个城市有路,那么就一定会有走回头路。
呢么要求权值最小的最短路程,就是让尽量回头路那段路程小。
假设把所有的道路都来回走一遍,找一次性能走的最长的一条路 ,那么剩下没走的就是最短的回头路了
用dfs找那条权值最大的那条路 因为要求这条路是不走回头路的,所以记录走到的点的父节点,不往父节点走
最后 最短路程为 sum * 2 - imax;
#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h> //INT_MAX
#include<bitset> // bitset<?> n
using namespace std; typedef long long LL;
typedef long long ll;
typedef pair<int,int> P;
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
const int INF =0x3f3f3f3f;
const int mod = 1e9+; const int MAXN = +;
LL ans = ;
LL dis[MAXN];
ll imax = ;
vector<pair<LL, LL> > V[MAXN]; void dfs(int pre,int fa){
for(int i = ; i < V[pre].size(); i++){
LL v = V[pre][i].first ;
LL d = V[pre][i].second;
if(v != fa){
dis[v] = dis[pre] + d;
dfs(v,pre);
}
}
imax = max(imax,dis[pre]);
} int main(){
int n ;
read(n);
LL x,y,z;
LL sum = ; for(int i = ; i< n; i++){
scanf("%lld%lld%lld",&x,&y,&z);
V[x].pb(mp(y,z));
V[y].pb(mp(x,z));
sum += z;
}
dis[] = ;
dfs(,);
print(sum * -imax);
}
旅行商问题【山财新生赛E】的更多相关文章
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- SCNU 2015ACM新生赛决赛【F. Oyk闯机关】解题报告
题目大意:一个$N$$\times$$N$的阵列,每个格子有$X_{ij}$个调和之音,若每次只能选择走右边或下边,从左上角出发走到右下角,问最多能收集到多少个调和之音? ...
- Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串
A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [ACTF2020 新生赛]BackupFile && [ACTF2020 新生赛]Upload &&[GYCTF2020]Blacklist
[ACTF2020 新生赛]BackupFile 尝试找到源代码,加上题目是备份文件,猜测备份文件里面有网站的源代码,御剑扫描一下,就扫到index.php 访问index.php.bak 下载源代码 ...
- [BUUCTF]REVERSE——[ACTF新生赛2020]Oruga
[ACTF新生赛2020]Oruga 附件 步骤: 例行检查,64位程序,无壳 64位ida载入,检索字符串,根据提示来到关键函数 14行~18行就是让字符串的前5位是 actf{ ,sub_78A( ...
- [BUUCTF]REVERSE——[ACTF新生赛2020]usualCrypt
[ACTF新生赛2020]usualCrypt 附件 步骤: 例行检查,无壳,32位程序 32位ida载入,直接看main函数 逻辑很简单,一开始让我们输入一个字符串,然后该字符串经过sub_4010 ...
- [BUUCTF]REVERSE——[ACTF新生赛2020]rome
[ACTF新生赛2020]rome 附件 步骤 无壳,32位程序 32位ida载入,根据提示字符串"You are correct!",找到关键函数func v15 = 'Q'; ...
- [BUUCTF]REVERSE——[ACTF新生赛2020]easyre
[ACTF新生赛2020]easyre 附件 步骤 查壳,32位程序,upx壳儿 脱完壳儿,扔进ida 分析 一开始给我们定义了一个数组, v4=[42,70,39,34,78,44,34,40,73 ...
- SCNU ACM 2016新生赛决赛 解题报告
新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...
随机推荐
- PHP乘法表
<?php //一行*$a="*";echo ("$a <br>"); //一行50个*for($j=1;$j<=50;$j++){ e ...
- Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- 软件常用设置(VC, eclipse ,nodejs)---自己备用
留存复制使用 1.VC ----1.1VC项目设置 输出目录: $(SolutionDir)../bin/$(platform)/$(Configuration) $(ProjectDir)../bi ...
- QT获取窗口句柄
winId()函数 SendMessage((HWND)(this->dlg->winId()),WM_SEND_MY_MESSAGE,0,0);
- <8>Cocos Creator组件开发cc.Component
1.组件简介 组件是Cocos Creator的主要构成,渲染(场景显示内容).逻辑.用户输入反馈.计时器等等几个方面都是由组件完成的.根据Cocos Creator的总体架构,组件和节点配合完成游戏 ...
- 【转】推荐4个不错的Python自动化测试框架
之前,开发团队接手一个项目并开始开发时,除了项目模块的实际开发之外,他们不得不为这个项目构建一个自动化测试框架.一个测试框架应该具有最佳的测试用例.假设(assumptions).脚本和技术来运行每一 ...
- quick-cocos2d-x 游戏开发之一】开发工具sublime text及其强力插件QuickXDev
http://blog.csdn.net/yihaiyiren/article/details/17220927
- c++学习笔记(二)-指针
1. 指向数组的指针 int balance[5] = { 1000, 2, 3, 17, 50 }; int *ptr; ptr = balance; //ptr是指向数组balance的指针 // ...
- python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出
想获得漂亮的格式化字符串后输出,可以使用json.dumps() 的indent 参数.它会使得输出和pprint() 函数效果类似 >>> data {'age': 4, 'nam ...
- onclick 常用手册
1.如何去使用onclick来跳转到我们指定的页面/跳转到指定url ☆如果只是在本页显示的话,可以直接用location, 方法如下: ①onclick="javascript:windo ...