結果

問題 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
コンパイル時間 478 ms
コンパイル使用メモリ 52,192 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 01:59:41
合計ジャッジ時間 1,224 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:14:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   14 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
main.cpp:15:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |     for(int i=0;i<N;i++){scanf("%d", S+i);}
      |                          ~~~~~^~~~~~~~~~~
main.cpp:17:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |     scanf("%d", &M);
      |     ~~~~~^~~~~~~~~~
main.cpp:20:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |         scanf("%d %d %d", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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