結果

問題 No.17 2つの地点に泊まりたい
ユーザー pessimist
提出日時 2025-06-09 19:36:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 965 bytes
コンパイル時間 2,276 ms
コンパイル使用メモリ 199,900 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-09 19:36:07
合計ジャッジ時間 3,788 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,i; cin>>N;
    vector<ll> cost(N);
    for(i=0;i<N;++i) cin>>cost[i];
    const ll INF = 1e15;
    vector<vector<ll>> dist(N, vector<ll>(N,INF));
    for(int i=0;i<N;++i) dist[i][i]=0;
    int M;cin>>M;
    for(i=0;i<M;++i){
        int a,b; ll c; cin>>a>>b>>c;
        dist[a][b]=min(dist[a][b], c);
        dist[b][a]=min(dist[b][a],c);
    }

    for(int k=0;k<N;++k){
        for(int i=0;i<N;++i){
            for(int j=0;j<N;++j){
                if(dist[i][k]==INF || dist[k][j]==INF) continue;
                dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);
            }
        }
    }
    // print__(dist, cost);

    ll ANS=INF;
    for(int i=1;i+1<N;++i) for(int j=1;j+1<N;++j) if(i!=j){
        ANS=min(ANS,dist[0][i]+cost[i]+dist[i][j]+cost[j]+dist[j][N-1]);
    }
    cout<<ANS<<endl;
    return 0;
}
0