Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
Description
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers u, vand c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and vis c.
It is guaranteed that the social network of the input forms a tree.
Output a single integer – the maximum sum of costs.
4
0 1 4
0 2 2
2 3 3
5
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
105
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
5551
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).
题意:通俗一点,求树的每条分支的和,把最大的拿出来
解法:dfs,遍历到叶子就比较一下喽
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int,int>>q[];
int sum;
int vis[];
void dfs(int x,int cot,int pre)
{
if(vis[x])
{
return;
}
vis[x]=;
for(int i=; i<q[x].size(); i++)
{
if(vis[q[x][i].first])
{
{
sum=max(sum,cot);
continue;
}
}
dfs(q[x][i].first,cot+q[x][i].second,x);
}
}
int main()
{
cin>>n;
for(int i=; i<n; i++)
{
int v,u,c;
cin>>v>>u>>c;
q[v].push_back({u,c});
q[u].push_back({v,c});
}
dfs(,,);
cout<<sum<<endl;
return ;
}
Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J的更多相关文章
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)
G. Fake News (easy) time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M
Description The marmots have prepared a very easy problem for this year's HC2 – this one. It involve ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A
Description Your search for Heidi is over – you finally found her at a library, dressed up as a huma ...
- Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)
http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...
- Helvetic Coding Contest 2018 online mirror (teams allowed, unrated)F3 - Lightsabers (hard)
题意:n个数字1-m,问取k个组成的set方案数 题解:假设某个数出现k次,那么生成函数为\(1+x+...+x^k\),那么假设第i个数出现ai次,结果就是\(\sum_{i=1}^m(1+x+.. ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- [Helvetic Coding Contest 2017 online mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...
- 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记
第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...
- Helvetic Coding Contest 2016 online mirror A1
Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...
随机推荐
- layer弹层
获得 layer 文件包后,解压并将 layer 整个文件夹(不要拆分结构) 存放到你项目的任意目录,使用时,只需引入 layer.js 即可. 注意:引入layer.js前必须先引入jquery1. ...
- Does Hadoop require SSH?
https://wiki.apache.org/hadoop/FAQ#Does_Hadoop_require_SSH.3F Hadoop provided scripts (e.g., start-m ...
- Android开发之开机自动启动应用
package com.raycloud.wolf.autostart; import android.content.BroadcastReceiver; import android.conten ...
- POJ2942 Knights of the Round Table 点双连通分量 二分图判定
题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...
- poj 1325 Machine Schedule 解题报告
题目链接:http://poj.org/problem?id=1325 题目意思:有 k 个作业,机器A有 n 个模式:0 ~ n-1,机器B 有 m 个模式:0~ m-1.每一个作业能运行在 A 的 ...
- Redmine 数据库连接错误
/******************************************************************** * Redmine 数据库连接错误 * 说明: * Open ...
- [Java] 读取文件
1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如 ...
- 怎么解决Failed to load the JNI shared library
怎么解决Failed to load the JNIshared library 解决Failed to load the JNIshared library唯一的方法就是重新安装eclipse, ...
- 洛谷P2473奖励关——状压DP
题目:https://www.luogu.org/problemnew/show/P2473 还是对DP套路不熟悉... 像这种前面影响后面,而后面不影响前面的问题就应该考虑倒序递推: 看n只有15那 ...
- 架构-MVVM:MVVM核心概念
ylbtech-架构-MVVM:MVVM核心概念 1.返回顶部 1. MVVM模式是Model.View.ViewModel的简称,最早出现在WPF,现在Silverlight中也使用该模式,MVVM ...