結果

問題 No.17 2つの地点に泊まりたい
ユーザー 小指が強い人小指が強い人
提出日時 2016-01-04 23:24:08
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,925 bytes
コンパイル時間 1,038 ms
コンパイル使用メモリ 83,964 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-19 15:10:44
合計ジャッジ時間 2,811 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <unordered_map>
#include <climits>
using namespace std;
typedef long long int64;

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

int min_cost(int start, int end) {
    Map2 root[50];
    for(int i = 0; i < n; i++) {
        root[i].cost = (int64)LONG_MAX;
        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;
        //if(cur == end)
        //    return root[end].cost;
        q.pop();
        for(auto c : cost[cur]) {
            int d = root[cur].cost + c.second;
            if(d < root[c.first].cost) {
                if(root[c.first].cost >= (int64)LONG_MAX - 1) {
                    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 = (int64)LONG_MAX;
    for(int i = 1; i < n - 1; i++)
        for(int j = 1; j < n - 1; j++) {
            if(i == j) continue;
            int64 t = 0;
            t += min_cost(0, i) + s[i];
            t += min_cost(i, j) + s[j];
            t += min_cost(j, n - 1);
            if(t < res)
                res = t;
        }
    cout << res << endl;
    return 0;
}
0