2012年每周一赛第一场第一题,注意到这是一棵树(因为N+1个节点只有N条边即可完全连通)。若选择第i个点作为终点,易知总长度为∑ci-di+si(就是赛时没观察到这点),其中c是一条路径的长度,d为起点到终点的长度,s为终点到学校的长度。换言之,只需要找出si-di的最小值即可。
Run Time: 0.32sec
Run Memory: 6816KB
Code Length: 1180Bytes
Submit Time: 2012-02-26 15:06:44
// Problem#: 4832
// Submission#: 1220695
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
#include <vector>
using namespace std;
struct Road {
int t;
int dist;
Road( int n, int d ) { t = n; dist = d; }
};
int N;
bool visited[ 100001 ] = { 0 };
int dist[ 100001 ], path[ 100001 ];
vector<Road> road[ 100001 ];
inline int min( int a, int b ) { return a < b ? a: b; }
void dfs( int n, int d ) {
for ( vector<Road>::iterator it = road[ n ].begin(); it != road[ n ].end(); it++ ) {
if ( !visited[ it->t ] ) {
path[ it->t ] = d + it->dist;
visited[ it->t ] = true;
dfs( it->t, path[ it->t ] );
}
}
}
int main()
{
int sum = 0;
int a, b, c, i;
scanf( "%d", &N );
for ( i = 0; i <= N; i++ )
scanf( "%d", &dist[ i ] );
for ( i = 1; i <= N; i++ ) {
scanf( "%d%d%d", &a, &b, &c );
road[ a ].push_back( Road( b, c ) );
road[ b ].push_back( Road( a, c ) );
sum += c;
}
path[ 0 ] = 0;
visited[ 0 ] = true;
dfs( 0, 0 );
int result = sum * 2 - path[ 0 ] + dist[ 0 ];
for ( i = 1; i <= N; i++ )
result = min( result, sum * 2 - path[ i ] + dist[ i ] );
printf( "%d\n", result );
return 0;
}