結果

問題 No.17 2つの地点に泊まりたい
ユーザー shisyamokongarishisyamokongari
提出日時 2016-10-06 15:56:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 389 ms / 5,000 ms
コード長 1,875 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 69,956 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 02:17:09
合計ジャッジ時間 3,420 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 14 ms
5,376 KB
testcase_04 AC 12 ms
5,376 KB
testcase_05 AC 163 ms
5,376 KB
testcase_06 AC 79 ms
5,376 KB
testcase_07 AC 50 ms
5,376 KB
testcase_08 AC 389 ms
5,376 KB
testcase_09 AC 372 ms
5,376 KB
testcase_10 AC 80 ms
5,376 KB
testcase_11 AC 162 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 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 9 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 138 ms
5,376 KB
testcase_24 AC 138 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 282 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

#define NMAX 50

typedef struct s{
	int to,c;
} Sed;

int main(){

	int N;
	int S[NMAX];
	int M;
	vector<Sed> ed[NMAX];
	queue<int> nq;

	cin>>N;
	for(int i=0;i<N;i++) cin>>S[i];

	cin>>M;
	for(int i=0;i<M;i++){
		int A,B,C;
		cin>>A>>B>>C;
		Sed tmpS;
		tmpS.c=C;
		tmpS.to=B;
		ed[A].push_back(tmpS);
		tmpS.to=A;
		ed[B].push_back(tmpS);
	}

	int ans=-1;

	for(int i=1;i<=N-2;i++){
		for(int j=i+1;j<=N-2;j++){
			int cost[4][NMAX];
			for(int k=0;k<4;k++){
				for(int l=0;l<NMAX;l++){
					cost[k][l]=-1;
				}
			}
			nq.push(0);
			cost[0][0]=0;
			while(!nq.empty()){
				int ni=nq.front();
				nq.pop();
				if(ni==i){
					if(cost[0][ni]!=-1){
						if(cost[1][ni]==-1||cost[0][ni]+S[ni]<cost[1][ni]){
							cost[1][ni]=cost[0][ni]+S[ni];
						}
					}
					if(cost[2][ni]!=-1){
						if(cost[3][ni]==-1||cost[2][ni]+S[ni]<cost[3][ni]){
							cost[3][ni]=cost[2][ni]+S[ni];
						}
					}
				}
				if(ni==j){
					if(cost[0][ni]!=-1){
						if(cost[2][ni]==-1||cost[0][ni]+S[ni]<cost[2][ni]){
							cost[2][ni]=cost[0][ni]+S[ni];
						}
					}
					if(cost[1][ni]!=-1){
						if(cost[3][ni]==-1||cost[1][ni]+S[ni]<cost[3][ni]){
							cost[3][ni]=cost[1][ni]+S[ni];
						}
					}
				}
				for(int k=0;k<ed[ni].size();k++){
					bool f=false;
					for(int l=0;l<4;l++){
						if(cost[l][ni]!=-1){
							if(cost[l][ed[ni][k].to]==-1||cost[l][ed[ni][k].to]>cost[l][ni]+ed[ni][k].c){
								f=true;
								cost[l][ed[ni][k].to]=cost[l][ni]+ed[ni][k].c;
							}
						}
					}
					if(f) nq.push(ed[ni][k].to);
				}
			}
			if(cost[3][N-1]!=-1){
				if(ans==-1||ans>cost[3][N-1]) ans=cost[3][N-1];
			}
			/*cout<<i<<","<<j<<"  "<<ans<<endl;
			for(int k=0;k<4;k++){
				for(int l=0;l<N;l++){
					cout<<k<<","<<l<<"---"<<cost[k][l]<<endl;

				}
			}*/
		}
	}

	cout<<ans<<endl;
}
0