結果

問題 No.17 2つの地点に泊まりたい
ユーザー 小指が強い人小指が強い人
提出日時 2016-01-05 00:27:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 344 ms / 5,000 ms
コード長 1,797 bytes
コンパイル時間 839 ms
コンパイル使用メモリ 83,388 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 02:09:39
合計ジャッジ時間 3,454 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 23 ms
5,376 KB
testcase_04 AC 9 ms
5,376 KB
testcase_05 AC 156 ms
5,376 KB
testcase_06 AC 68 ms
5,376 KB
testcase_07 AC 36 ms
5,376 KB
testcase_08 AC 337 ms
5,376 KB
testcase_09 AC 344 ms
5,376 KB
testcase_10 AC 65 ms
5,376 KB
testcase_11 AC 148 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 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 13 ms
5,376 KB
testcase_19 AC 7 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 20 ms
5,376 KB
testcase_23 AC 126 ms
5,376 KB
testcase_24 AC 125 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 269 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <unordered_map>
#include <climits>
using namespace std;
typedef long long int64;
const long long MAX_COST = (LLONG_MAX >> 6);

struct Map1
{
    int id;
	int64 cost;
	bool operator<(const Map1& m) const {
	    return cost > m.cost;
	}
};
struct Map2
{
	int64 cost = MAX_COST;
	int prev = -1;
};
int s[50] = {0};
unordered_map<int, unordered_map<int, int64>> cost;
int n, m;

int64 min_cost(int start, int end) {
    Map2 root[50];
    for(int i = 0; i < n; i++) {
        root[i].cost = MAX_COST;
        root[i].prev = -1;
    }
    priority_queue<Map1> q;
    root[start].cost = 0;
    for(auto c : cost[start]) {
        Map1 m = {c.first, c.second};
        root[c.first].prev = start;
        root[c.first].cost = c.second;
        q.push(m);
    }
    while(!q.empty()) {
        int cur = q.top().id;
        q.pop();
        for(auto c : cost[cur]) {
            int64 d = root[cur].cost + c.second;
            if(d < root[c.first].cost) {
                Map1 m = {c.first, d};
                q.push(m);
                root[c.first].prev = cur;
                root[c.first].cost = d;
            }
        }
    }
    return root[end].cost;
}

int main(void)
{
    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;
        cost[b][a] = cost[a][b] = c;
    }
    int64 res = MAX_COST;
    for(int i = 1; i < n - 1; i++)
        for(int j = 1; j < n - 1; j++) {
            if(i == j) continue;
            int64 t = s[i] + s[j];
            t += min_cost(0, i);
            t += min_cost(i, j);
            t += min_cost(j, n - 1);
            if(t < res)
                res = t;
        }
    cout << res << endl;
    return 0;
}
0