結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
shisyamokongari
|
| 提出日時 | 2016-10-06 15:56:55 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 364 ms / 5,000 ms |
| コード長 | 1,875 bytes |
| コンパイル時間 | 709 ms |
| コンパイル使用メモリ | 68,324 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-15 01:38:32 |
| 合計ジャッジ時間 | 3,314 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
#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;
}
shisyamokongari