結果
問題 |
No.17 2つの地点に泊まりたい
|
ユーザー |
![]() |
提出日時 | 2017-05-01 13:54:38 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 18 ms / 5,000 ms |
コード長 | 1,440 bytes |
コンパイル時間 | 560 ms |
コンパイル使用メモリ | 55,808 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-15 01:40:16 |
合計ジャッジ時間 | 1,501 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 27 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:106:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 106 | scanf("%d", &point[i].s); | ~~~~~^~~~~~~~~~~~~~~~~~~ main.cpp:108:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 108 | scanf("%d", &m); | ~~~~~^~~~~~~~~~ main.cpp:111:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 111 | scanf("%d%d%d", &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include<cstdio> #include<vector> #include<queue> #include<map> using namespace std; typedef long long ll; typedef pair<int,int> pii; struct road_t { int to, c; }; struct point_t { int s; vector<road_t> road; }; struct state_t { int c, p, s; bool operator < (const state_t& rhs) const { return c>rhs.c; } }; priority_queue<state_t> que; int min_u(int& m, int v) { if(m>v) { m=v; return 1; } return 0; } void push(int c, int p, int s) { que.push((state_t){c, p, s}); } int pop(int& c, int& p, int& s) { if(que.empty()) return 0; state_t w=que.top(); c=w.c; p=w.p; s=w.s; que.pop(); return 1; } int solve(vector<point_t>& point) { int c=0, p=0, s=0, n=point.size(); vector<int> dp(n*n, 50*1000*2); push(c, p, s); for(;pop(c, p, s);) { int idx=s*n+p; if(min_u(dp[idx], c)==0) continue; for(auto r: point[p].road) { push(c+r.c, r.to, s); } if(p==0 || p==n-1 || s==n-1 || s==p) continue; int ns=p; if(s>0) ns=n-1; push(c+point[p].s, p, ns); } return dp.back(); } int main(void) { int n, m, i, a, b, c; vector<point_t> point; while(scanf("%d", &n)==1) { point.clear(); point.resize(n); for(i=0;i<n;i++) { scanf("%d", &point[i].s); } scanf("%d", &m); for(i=0;i<m;i++) { scanf("%d%d%d", &a, &b, &c); point[a].road.push_back((road_t){b, c}); point[b].road.push_back((road_t){a, c}); } printf("%d\n", solve(point)); } return 0; }