結果

問題 No.17 2つの地点に泊まりたい
ユーザー krotonkroton
提出日時 2015-07-05 12:36:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,330 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 68,980 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 02:05:20
合計ジャッジ時間 1,664 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const int INF = 1 << 25;

int n;
int s[50];
vector<pair<int,int>> g[50];

int d[16384];
inline int packer(int now, int cnt, int pre){
	if(cnt != 1)pre = 0;
	return (((pre<<2)|cnt)<<6)|now;
}

int dijkstra(){
	fill(begin(d), end(d), INF);
	d[0] = 0;

	priority_queue<int, vector<int>, greater<int>> pq;
	pq.push(0);

	while(!pq.empty()){
		int pack = pq.top(); pq.pop();
		int now = pack & 63; pack >>= 6;
		int cnt = pack & 3;  pack >>= 2;
		int pre = pack & 63; pack >>= 6;
		int cost = pack;

		if(now == n - 1 && cnt == 2){
			return cost;
		}

		for(auto ps : g[now]){
			int nxt = ps.first;
			int nxt_cost = cost + ps.second;
			
			int p = packer(nxt, cnt, pre);
			if(nxt_cost < d[p]){
				d[p] = nxt_cost;
				pq.push((nxt_cost<<14)|p);
			}

			if(nxt == 0 || nxt == n - 1){
				continue;
			}
			if(cnt == 0 || (cnt == 1 && nxt != pre)){
				int p = packer(nxt, cnt + 1, nxt);
				int nc = nxt_cost + s[nxt];
				if(nc < d[p]){
					d[p] = nc;
					pq.push((nc<<14)|p);
				}
			}
		}
	}
	return INF;
}

int main(){
	cin >> n;
	for(int i=0;i<n;i++)cin >> s[i];

	int m;
	cin >> m;
	for(int i=0;i<m;i++){
		int a, b, c;
		cin >> a >> b >> c;

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

	cout << dijkstra() << endl;
	return 0;
}
0