結果

問題 No.17 2つの地点に泊まりたい
ユーザー tottoripapertottoripaper
提出日時 2014-11-16 08:36:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,518 bytes
コンパイル時間 483 ms
コンパイル使用メモリ 55,944 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-05 03:30:02
合計ジャッジ時間 1,613 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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