結果
問題 | No.17 2つの地点に泊まりたい |
ユーザー |
![]() |
提出日時 | 2015-04-28 20:25:43 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,392 bytes |
コンパイル時間 | 462 ms |
コンパイル使用メモリ | 54,248 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-15 01:20:12 |
合計ジャッジ時間 | 1,234 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
コンパイルメッセージ
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; } | ~~~~~^~~~~~~~~~
ソースコード
#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; }