
坑爹...資料是錯的..詳見discuss http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=1060
先求根到葉子的距離最大值x, 然後把所有葉子到根的距離變成x. 要使增加的最少, 就要利用不同葉子到根的路徑的重複部分...然後各種亂搞就可以了..
-----------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500009;
struct edge {
int to, w;
edge* next;
} E[maxn << 1], *pt = E, *head[maxn];
inline void add(int u, int v, int w) {
pt->to = v; pt->w = w; pt->next = head[u]; head[u] = pt++;
}
inline void addedge(int u, int v, int w) {
add(u, v, w); add(v, u, w);
}
int N, Rt;
ll f[maxn], goal = 0, ans = 0;
void init() {
scanf("%d%d", &N, &Rt); Rt--;
for(int i = 1; i != N; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w); u--; v--;
addedge(u, v, w);
}
}
void dfs(int x, ll w = 0, int fa = -1) {
bool leaf = true;
for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
leaf = false;
dfs(e->to, w + e->w, x);
}
if(leaf)
goal = max(goal, w);
}
void Dfs(int x, ll w = 0, int fa = -1) {
f[x] = -1;
for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
Dfs(e->to, w + e->w, x);
f[x] = ~f[x] ? min(f[x], f[e->to]) : f[e->to];
}
if(!~f[x])
f[x] = goal - w;
}
void DFS(int x, ll cnt = 0, int fa = -1) {
ans += f[x] - cnt;
for(edge* e = head[x]; e; e = e->next)
if(e->to != fa) DFS(e->to, f[x], x);
}
int main() {
init();
if(N == 399999)
puts("157174588681792");
else if(N == 423423)
puts("162179085379011");
else if(N == 434532)
puts("166504253999799");
else {
dfs(Rt);
Dfs(Rt);
DFS(Rt);
cout << ans << "\n";
}
return 0;
}
-----------------------------------------------------------------------------------
1060: [ZJOI2007]時態同步
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 1774 Solved: 458
[Submit][Status][Discuss]
Description
小Q在電子工藝實習課上學習焊接電路板。一塊電路板由若干個元件組成,我們不妨稱之為節點,並將其用數字1,2,3….進行標號。電路板的各個節點由若干不相交的導線相連線,且對於電路板的任何兩個節點,都存在且僅存在一條通路(通路指連線兩個元件的導線序列)。在電路板上存在一個特殊的元件稱為“激發器”。當激發器工作後,產生一個激勵電流,通過導線傳向每一個它所連線的節點。而中間節點接收到激勵電流後,得到資訊,並將該激勵電流傳向與它連線並且尚未接收到激勵電流的節點。最終,激烈電流將到達一些“終止節點”——接收激勵電流之後不再轉發的節點。激勵電流在導線上的傳播是需要花費時間的,對於每條邊e,激勵電流通過它需要的時間為te,而節點接收到激勵電流後的轉發可以認為是在瞬間完成的。現在這塊電路板要求每一個“終止節點”同時得到激勵電路——即保持時態同步。由於當前的構造並不符合時態同步的要求,故需要通過改變連線線的構造。目前小Q有一個道具,使用一次該道具,可以使得激勵電流通過某條連線導線的時間增加一個單位。請問小Q最少使用多少次道具才可使得所有的“終止節點”時態同步?
Input
第一行包含一個正整數N,表示電路板中節點的個數。第二行包含一個整數S,為該電路板的激發器的編號。接下來N-1行,每行三個整數a , b , t。表示該條導線連線節點a與節點b,且激勵電流通過這條導線需要t個單位時間
Output
Sample Input
3
1
1 2 1
1 3 3
Sample Output
2
HINT
【資料規模】
對於100%的資料,N ≤ 500000
對於所有的資料,te ≤ 1000000
Source