很好的一道思维题。
我最开始的思路是判断这个题目是由初始状态以最小的代价和转换为合理的状态。这显然理解错了,因为题目说可以同时移动ORZ。。。
然后弄清楚题意后显然是一个二分。
贪心性质:越往上走越好。
走到1节点的儿子节点的时候如果(该儿子节点的所有点的最小剩余距离)大于儿子节点距离根的距离的2倍的话,那么这个点就可以继续向上走。(这点我没想到啊。。。)我最开始的做法就是剩一个最小的,其他的继续向上走,然而这样是不对的。。。
然后这题我TM调了一晚上,猜哪错了?
这步我最开始先写的x = y,这SB错误我都犯过一百回了好吗,就是不长记性啊。。。。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long lint ;
typedef pair<lint,lint> pii;
const lint maxn = 500005;
const lint maxm = 1000005;
lint ver[maxm],ne[maxm],cost[maxm],he[maxn],tot,a[maxn];
void add( lint x,lint y,lint w ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
cost[tot ] = w;
}
void init(){
memset( he,0,sizeof(he) );
tot = 1;
}
queue<lint> que;
LL d[maxn],dis[maxn][30],f[maxn][30];
void build(){
que.push(1);
d[1] = 0;
while( que.size() ){
lint x = que.front();
que.pop();
for( lint cure = he[x];cure;cure = ne[cure] ){
lint y = ver[cure];
if( y == f[x][0] ) continue;
que.push(y);
f[y][0] = x; d[y] = d[x] + 1;
dis[y][0] = cost[cure];
for( lint i = 1;(1<<i) <= d[y];i++ ){
f[y][i] = f[ f[y][i-1] ][i-1];
dis[y][i] = dis[y][i-1] + dis[ f[y][i-1] ][i-1];
}
}
}
}
lint n,m,vis[maxn],du[maxn],rest[maxn],summ[maxn];
vector<lint> ve1;
vector<pii> ve2;
vector<pii> ve3;
lint dfs( lint x,lint f,lint cnt ){
lint sum = n+10;
cnt += vis[x];
if( du[x] == 1 && x != 1 ){
summ[x] = cnt;
if( d[x] == 1 && !cnt ){
ve1.push_back(x);
}
return cnt;
}
for( lint cure = he[x];cure;cure = ne[cure] ){
lint y = ver[cure];
if( y ==f ) continue;
sum = min( sum, dfs( y,x,cnt ));
}
if( d[x] == 1 ){
if( sum == 0 ){
ve1.push_back(x);
}
}
summ[x] = sum;
return sum;
}
bool cmp1( lint x,lint y ){
return dis[x][0] < dis[y][0];
}
bool cmp2( pii& x,pii& y ){
if( x.second == y.second ){
return x.first < y.first;
}
return x.second > y.second;
}
bool cmp3( pii& x,pii& y ){
if( x.second == y.second ){
return x.first < y.first;
}
return x.second < y.second;
}
bool judge( LL mid ){
for( int i = 1;i <= n;i++ ) vis[i] = 0;
ve1.clear(); ve2.clear();ve3.clear();
for( lint i = 1;i <= m;i++ ){
lint x = a[i];
lint len = mid;
for( lint i = 25;i >= 0;i-- ){
lint y = f[x][i];
if( y >= 2 && dis[x][i] <= len ){
len -= dis[x][i]; x = y;
}
}
len -= dis[x][0];
if( d[x] == 1 ){
ve2.push_back( pii( x, len) );
}
vis[x] ++;
}
dfs( 1,0,0 );
//sort( ve1.begin(),ve1.end(),cmp1 );
sort( ve2.begin(),ve2.end(),cmp2 );
// memset( vis,0,sizeof( vis ) );
for( int i = 0;i < ve2.size();i++ ){
pii x = ve2[i];
if( summ[x.first] > 1 ) {
ve3.push_back(x);
summ[ x.first ]--;
continue;
}
if( x.second > dis[ x.first ][0] ){
ve3.push_back( x );
summ[ x.first ]--;
if( !summ[x.first] ){
ve1.push_back( x.first );
}
}
}
sort( ve1.begin(),ve1.end(),cmp1 );
sort( ve3.begin(),ve3.end(),cmp3 );
if( mid == 21462 ){
cout << "debug" << endl;
}
if( ve1.size() > ve3.size() ) return false;
lint l = 0;
for( lint i = 0;i < ve1.size();i++ ){
if( l >= ve3.size() ) return false;
lint x = ve1[i];
while( ve3[l].second < dis[x][0] && l < ve3.size() ) l++;
if( l >= ve3.size() ) return false;
l++;
}
return true;
}
int main(){
init();
LL r = 0;
scanf("%lld",&n);
for( lint x,y,w,i = 1;i <= n-1;i++ ){
scanf("%lld%lld%lld",&x,&y,&w);
add( x,y,w );add( y,x,w );
r += w;
du[x]++;
du[y]++;
}
r++;
LL ini = r;
build();
scanf("%lld",&m);
for( lint i = 1;i <= m;i++ ) scanf("%lld",&a[i]);
LL l = -1;
while( l != r-1 ){
LL mid = l + r>>1;
if( judge(mid) ){
r = mid;
}else{
l = mid;
}
}
if( r != ini )
cout << r;
else cout << -1;
return 0;
}