JZOJ 5406. 【NOIP2017提高A组模拟10.10】Tree
来源:互联网 发布:mysql主键从1开始 编辑:程序博客网 时间:2024/05/29 18:11
Description
给定一颗n 个点的树,树边带权,试求一个排列P,使下式的值最大
其中maxflow(s; t) 表示从点s 到点t 之间的最大流,即从s 到t 的路径上最小的边权
Input
第一行一个整数n,表示点数
下接n - 1 行,每行三个数u, v, w 表示一条连接点u 和点v 权值为w 的边
Output
输出一行一个整数,表示答案
Sample Input
2
1 2 2333
Sample Output
2333
Data Constraint
对于前5% 的数据满足n <= 8
对于前40% 的数据满足n <= 200
对于前60% 的数据满足n <= 2000
对于100% 的数据满足n <= 100000
Solution
你绝对不会想到这道题的结论有多么简洁!——答案为所有边的边权和!
什么?为什么呢?
首先,我们不能避免计算最短边的答案,总会经过的。
同样,次短边也要加进去,因为
N 个点的排列不得不经过这条边。同理,第三小、第四小……最大边,都要加入答案,因为答案为
n−1 条边之和。
-于是——答案为所有边的边权和。
Code
#include<cstdio>using namespace std;long long ans;inline int read(){ int X=0,w=1; char ch=0; while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();} while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar(); return X*w;}int main(){ int n=read(); while(--n) { read(),read(); ans+=read(); } printf("%lld",ans); return 0;}
阅读全文
1 0
- JZOJ 5406. 【NOIP2017提高A组模拟10.10】Tree
- JZOJ 5404. 【NOIP2017提高A组模拟10.10】Graph
- 【JZOJ 5405】【NOIP2017提高A组模拟10.10】Permutation
- JZOJ 5405.【NOIP2017提高A组模拟10.10】Permutation
- JZOJ 5404. 【NOIP2017提高A组模拟10.10】Graph
- JZOJ 5405. 【NOIP2017提高A组模拟10.10】Permutation
- 【JZOJ 5438】【NOIP2017提高A组集训10.31】Tree
- JZOJ 5438. 【NOIP2017提高A组集训10.31】Tree
- JZOJ 5438. 【NOIP2017提高A组集训10.31】Tree
- [jzoj5406]【NOIP2017提高A组模拟10.10】Tree
- 【JZOJ 4931】【NOIP2017提高组模拟12.24】A
- 【JZOJ 4931】【NOIP2017提高组模拟12.24】A
- JZOJ 5195. 【NOIP2017提高组模拟7.3】A
- JZOJ 100026. 【NOIP2017提高A组模拟7.7】图
- JZOJ 100030. 【NOIP2017提高A组模拟7.8】为了爱情
- jzoj. 100031. 【NOIP2017提高A组模拟7.9】外星密码
- JZOJ 100035【NOIP2017提高A组模拟7.10】区间
- JZOJ 100036 【NOIP2017提高A组模拟7.10】随机
- caffe中Python层的使用
- 计算机各层网络协议
- 【tensorflow】tensor连接操作concat
- 文件系统(一)
- 对象引用之String
- JZOJ 5406. 【NOIP2017提高A组模拟10.10】Tree
- 剑指offer第16题(合并两个排序的链表)
- WebService服务及客户端调用简单实例
- HDU4745:Two Rabbits(区间DP)
- 最短路Floyd、Dijkstra
- sip概念
- Jzoj4627 斐波那契数列
- POJ 3207 Ikki's Story IV
- Unity使用第三方C语言库思路小结