結果

問題 No.17 2つの地点に泊まりたい
ユーザー shisyamokongarishisyamokongari
提出日時 2016-10-06 15:56:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 409 ms / 5,000 ms
コード長 1,875 bytes
コンパイル時間 604 ms
コンパイル使用メモリ 67,240 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-05 03:49:35
合計ジャッジ時間 3,762 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 14 ms
4,380 KB
testcase_04 AC 12 ms
4,380 KB
testcase_05 AC 169 ms
4,376 KB
testcase_06 AC 83 ms
4,380 KB
testcase_07 AC 53 ms
4,376 KB
testcase_08 AC 409 ms
4,384 KB
testcase_09 AC 398 ms
4,376 KB
testcase_10 AC 85 ms
4,380 KB
testcase_11 AC 169 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 15 ms
4,376 KB
testcase_23 AC 145 ms
4,380 KB
testcase_24 AC 146 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 299 ms
4,380 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