結果
| 問題 | No.17 2つの地点に泊まりたい |
| コンテスト | |
| ユーザー |
y_mazun
|
| 提出日時 | 2015-04-28 20:25:43 |
| 言語 | C++11 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 5,000 ms |
| + 742µs | |
| コード長 | 1,392 bytes |
| 記録 | |
| コンパイル時間 | 378 ms |
| コンパイル使用メモリ | 75,164 KB |
| 実行使用メモリ | 5,888 KB |
| 最終ジャッジ日時 | 2026-07-17 04:11:31 |
| 合計ジャッジ時間 | 1,775 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#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;
}
y_mazun