結果

問題 No.17 2つの地点に泊まりたい
ユーザー y_mazuny_mazun
提出日時 2015-04-28 20:25:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,392 bytes
コンパイル時間 429 ms
コンパイル使用メモリ 54,272 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 02:02:14
合計ジャッジ時間 1,178 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 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 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int getInt()’:
main.cpp:8:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 | inline int getInt(){ int s; scanf("%d", &s); return s; }
      |                             ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <cstring>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <queue>
#include <cstdio>
#include <set>

inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int dp[50][64];

int main(){
  const int n = getInt();
  vector<int> s(n);
  REP(i,n) s[i] = getInt();

  const int m = getInt();
  vector<vector<pair<int, int> > > g(n);

  REP(i,m){
    const int a = getInt();
    const int b = getInt();
    const int c = getInt();

    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }

  typedef pair<int, pair<int, int> > data;
  priority_queue<data, vector<data>, greater<data> > pq;

  memset(dp, -1, sizeof(dp));
  pq.push(data(0, make_pair(0, n)));

  while(pq.size()){
    const data d = pq.top(); pq.pop();
    const int cost = d.first;
    const int pos  = d.second.first;
    const int stay = d.second.second;

    if(dp[pos][stay] != -1) continue;
    dp[pos][stay] = cost;

    if(pos == n - 1 && stay == n + 1){
      printf("%d\n", cost);
      return 0;
    }

    if(stay <= n && stay != pos && pos != 0 && pos != n - 1){
      pq.push(data(cost + s[pos], make_pair(pos, stay == n ? pos : n + 1)));
    }

    REP(i,g[pos].size()){
      const int next = g[pos][i].first;
      const int cc = cost + g[pos][i].second;

      if(dp[next][stay] == -1)
	pq.push(data(cc, make_pair(next, stay)));
    }
  }

  return 0;
}
0