結果

問題 No.17 2つの地点に泊まりたい
コンテスト
ユーザー tottoripaper
提出日時 2014-11-16 08:36:05
言語 C++11
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 5,000 ms
+ 812µs
コード長 1,518 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 366 ms
コンパイル使用メモリ 73,652 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-17 04:10:04
合計ジャッジ時間 1,702 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <algorithm>
#include <queue>

#define mp std::make_pair

typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

int N, M, S[50];
std::vector<P> G[50];

int main(){
    scanf("%d", &N);
    for(int i=0;i<N;i++){scanf("%d", S+i);}

    scanf("%d", &M);
    for(int i=0;i<M;i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);

        G[a].push_back(mp(b, c));
        G[b].push_back(mp(a, c));
    }

    int d[50][52];
    std::fill(&d[0][0], &d[0][0]+50*52, 1001001001);
    d[0][50] = 0;
    std::priority_queue<State, std::vector<State>, std::greater<State> > q;
    q.push(mp(0, mp(0, 50)));

    while(!q.empty()){
        State s = q.top(); q.pop();
        int cost = s.first, from = s.second.first, state = s.second.second;

        if(state == 50 && from != 0 && from != N-1 && d[from][from] > cost + S[from]){
            d[from][from] = cost + S[from];
            q.push(mp(d[from][from], mp(from, from)));
        }
        if(0 <= state && state < N && from != 0 && from != N-1 && from != state && d[from][51] > cost + S[from]){
            d[from][51] = cost + S[from];
            q.push(mp(d[from][51], mp(from, 51)));
        }

        for(auto e : G[from]){
            if(d[e.first][state] > cost + e.second){
                d[e.first][state] = cost + e.second;
                q.push(mp(d[e.first][state], mp(e.first, state)));
            }
        }
    }

    printf("%d\n", d[N-1][51]);
}
0