Sicily 4832. Paper Route

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;

}                                 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值