結果

問題 No.17 2つの地点に泊まりたい
ユーザー 0w10w1
提出日時 2016-11-22 18:15:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,516 bytes
コンパイル時間 1,995 ms
コンパイル使用メモリ 187,692 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 02:17:22
合計ジャッジ時間 2,734 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< vvi > vvvi;
typedef vector< vvvi > vvvvi;
typedef vector< vvvvi > vvvvvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef vector< vvl > vvvl;
typedef vector< vvvl > vvvvl;
typedef vector< vvvvl > vvvvvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< vp > vvp;
typedef vector < string > vs;
typedef vector< vs > vvs;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< vvd > vvvd;
typedef vector< bool > vb;
typedef vector< vb > vvb;

const int INF = 0x3f3f3f3f;
const int MOD7 = ( int ) 1e9 + 7;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
    if( x > v ){
        x = v;
        return 1;
    }
    return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
    if( x < v ){
        x = v;
        return 1;
    }
    return 0;
}

typedef tuple< int, int, int, int > t4i;

int N;
vi S;
int M;
vvp E;

void init(){
    cin >> N;
    S = vi( N );
    for( int i = 0; i < N; ++i )
        cin >> S[ i ];
    cin >> M;
    E = vvp( N );
    for( int i = 0; i < M; ++i ){
        int A, B, C; cin >> A >> B >> C;
        E[ A ].emplace_back( C, B );
        E[ B ].emplace_back( C, A );
    }
}

vvvi dp;

void preprocess(){
    dp = vvvi( N + 1, vvi( N + 1, vi( N, INF ) ) );
}

void solve(){
    priority_queue< t4i, vector< t4i >, greater< t4i > > pq;
    pq.emplace( dp[ N ][ N ][ 0 ] = 0, N, N, 0 );
    while( not pq.empty() ){
        int d, f, s, u; tie( d, f, s, u ) = pq.top(); pq.pop();
        if( f < N and s < N and u == N - 1 )
            cout << d << endl, exit( 0 );
        if( d != dp[ f ][ s ][ u ] ) continue;
        for( int i = 0; i < E[ u ].size(); ++i ){
            int c, v; tie( c, v ) = E[ u ][ i ];
            if( upmin( dp[ f ][ s ][ v ], dp[ f ][ s ][ u ] + c ) )
                pq.emplace( dp[ f ][ s ][ v ], f, s, v );
            if( f == N and v != N - 1 and v != 0 ){
                if( upmin( dp[ v ][ s ][ v ], dp[ f ][ s ][ u ] + c + S[ v ] ) )
                    pq.emplace( dp[ v ][ s ][ v ], v, s, v );
            } else if( s == N and f != v and v != N - 1 and v != 0 ){
                if( upmin( dp[ v ][ f ][ v ], dp[ f ][ s ][ u ] + c + S[ v ] ) )
                    pq.emplace( dp[ v ][ f ][ v ], v, f, v );
            }
        }
    }
}

signed main(){
    ios::sync_with_stdio( 0 );
    init();
    preprocess();
    solve();
    return 0;
}
0