JZOJ 5406. 【NOIP2017提高A组模拟10.10】Tree
Description
給定一顆n 個(gè)點(diǎn)的樹,樹邊帶權(quán),試求一個(gè)排列P,使下式的值最大
其中maxflow(s; t) 表示從點(diǎn)s 到點(diǎn)t 之間的最大流,即從s 到t 的路徑上最小的邊權(quán)
Input
第一行一個(gè)整數(shù)n,表示點(diǎn)數(shù)
下接n - 1 行,每行三個(gè)數(shù)u, v, w 表示一條連接點(diǎn)u 和點(diǎn)v 權(quán)值為w 的邊
Output
輸出一行一個(gè)整數(shù),表示答案
Sample Input
2
1 2 2333
Sample Output
2333
Data Constraint
對(duì)于前5% 的數(shù)據(jù)滿足n <= 8
對(duì)于前40% 的數(shù)據(jù)滿足n <= 200
對(duì)于前60% 的數(shù)據(jù)滿足n <= 2000
對(duì)于100% 的數(shù)據(jù)滿足n <= 100000
Solution
你絕對(duì)不會(huì)想到這道題的結(jié)論有多么簡(jiǎn)潔!——答案為所有邊的邊權(quán)和!
什么?為什么呢?
首先,我們不能避免計(jì)算最短邊的答案,總會(huì)經(jīng)過的。
同樣,次短邊也要加進(jìn)去,因?yàn)?N 個(gè)點(diǎn)的排列不得不經(jīng)過這條邊。
同理,第三小、第四小……最大邊,都要加入答案,因?yàn)榇鸢笧?n?1 條邊之和。
-于是——答案為所有邊的邊權(quán)和。
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; }總結(jié)
以上是生活随笔為你收集整理的JZOJ 5406. 【NOIP2017提高A组模拟10.10】Tree的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JZOJ 5401. 【NOIP2017
- 下一篇: JZOJ 5404. 【NOIP2017