結果

問題 No.17 2つの地点に泊まりたい
ユーザー Lay_ecLay_ec
提出日時 2015-06-26 10:18:56
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,443 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 94,272 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 00:46:04
合計ジャッジ時間 2,473 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <map>
#include <queue>
#include <stack>

using namespace std;

struct edge{int to,cost;};
typedef pair<int,int> P;
typedef pair<int,P> PP;
vector<vector<edge> > G; 

int main()
{
		
	int n;
	cin>>n;
	
	G.resize(n);
	
	vector<int> stay(n);
	for(int i=0;i<n;i++) cin>>stay[i];
	
	int m;
	cin>>m;
	for(int i=0;i<m;i++){
		int a,b,c;
		cin>>a>>b>>c;
		G[a].push_back(edge{b,c});
		G[b].push_back(edge{a,c});
	}
	
	//d[今の地点][これまで何回泊まったか]=費用
	vector<vector<int> > d(n,vector<int>(3,1000000));
	priority_queue<PP,vector<PP>,greater<PP> > q;
	d[0][0]=0;
	q.push(PP(0,P(0,0)));
	
	vector<bool> stayed(n,false);
	while(!q.empty()){
		PP pp=q.top(); q.pop();
		int now=pp.second.first;
		int cnt=pp.second.second;
		if(pp.first>d[now][cnt]) continue;
				
		for(int i=0;i<G[now].size();i++){
			int to=G[now][i].to;
			int cost=G[now][i].cost;
			if(d[to][cnt]>d[now][cnt]+cost){
				d[to][cnt]=d[now][cnt]+cost;
				q.push(PP(d[to][cnt],P(to,cnt)));
			}
		}
		
		if(cnt<2 && !stayed[now] && now!=0 && now!=n-1 && d[now][cnt+1]>d[now][cnt]+stay[now]){
			stayed[now]=true;
			d[now][cnt+1]=d[now][cnt]+stay[now];
			q.push(PP(d[now][cnt+1],P(now,cnt+1)));
		}
		
	}
	cout<<d[n-1][2]<<endl;
	
	
	return 0;
}
0