結果

問題 No.17 2つの地点に泊まりたい
ユーザー 👑 jupirojupiro
提出日時 2020-07-10 20:03:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,600 bytes
コンパイル時間 1,341 ms
コンパイル使用メモリ 134,392 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-01 15:47:52
合計ジャッジ時間 2,679 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

template<typename T>
vector<T> dijkstra_V2(int start, vector<vector<T>>& dist) {
    int n = dist.size();
    vector<T> res(n, INF);
    queue<int> q;
    q.emplace(start);
    vector<bool> used(n, false);
    res[0] = 0;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        used[cur] = true;
        for (int i = 0; i < n; i++) chmin(res[i], res[cur] + dist[cur][i]);
        int next = -1;
        T d = INF;
        for (int i = 0; i < n; i++) if (!used[i]) if (chmin(d, res[i])) next = i;
        if (next != -1) q.emplace(next);
    }
    return res;
}
void warshallFloyd(ll n, vector<vector<ll>>& d) {
    for (ll k = 0; k < n; k++) {
        for (ll i = 0; i < n; i++) {
            if (d[i][k] == INF)
                continue;
            for (ll j = 0; j < n; j++) {
                if (d[k][j] == INF)
                    continue;
                chmin(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	ll n; scanf("%lld", &n);
	vector<ll> s(n); for (int i = 0; i < n; i++) scanf("%lld", &s[i]);
	ll m; scanf("%lld", &m);
    vector<vector<ll>> d(n, vector<ll>(n, INF)); for (int i = 0; i < n; i++) d[i][i] = 0;
    for (int i = 0; i < m; i++) {
        ll a, b, c; scanf("%lld %lld %lld", &a, &b, &c);
        d[a][b] = d[b][a] = c;
    }
    warshallFloyd(n, d);
    ll res = INF;
    for (int i = 1; i < n - 1; i++) for (int j = 1; j < n - 1; j++) if (i != j) {
        ll len = d[0][i] + d[i][j] + d[j][n - 1];
        chmin(res, len + s[i] + s[j]);
    }
    cout << res << endl;
	return 0;
}
0