結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 小指が強い人小指が強い人
提出日時 2016-01-11 14:03:32
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,146 bytes
コンパイル時間 962 ms
コンパイル使用メモリ 90,268 KB
実行使用メモリ 8,036 KB
最終ジャッジ日時 2023-10-19 22:48:00
合計ジャッジ時間 7,791 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <climits>
#include <string>
using namespace std;
typedef long long int64;
const int64 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;
    unordered_map<int, bool> prev;
};
unordered_map<int, unordered_map<int, int64>> cost;
Map2 root[200];
int n, m, s, g;

int64 min_cost(int start, int end) {
    for(int i = 0; i < n; i++)
        root[i].cost = MAX_COST;
    priority_queue<Map1> q;
    root[start].cost = 0;
    for(auto c : cost[start]) {
        Map1 m = {c.first, c.second};
        root[c.first].prev[start] = true;
        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.clear();
                root[c.first].prev[cur] = true;
                root[c.first].cost = d;
            }
            else if(d == root[c.first].cost) {
                //Map1 m = {c.first, d};
                //q.push(m);
                root[c.first].prev[cur] = true;
            }
        }
    }
    return root[end].cost;
}

string enumerate(int i)
{
    int size = root[i].prev.size();
    if(size == 0)
        return to_string(i);
    string res = "a";
    char c = '0' + i;
    for(auto p : root[i].prev) {
        string str = enumerate(p.first) + c;
        if(str < res)
            res = str;
    }
    return res;
}

int main(void)
{
    cin >> n >> m >> s >> g;
    for(int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        cost[b][a] = cost[a][b] = c;
    }
    min_cost(s, g);
    string res = enumerate(g);
    for(int i = 0; i < res.size(); i++)
        if(i == 0)
            cout << res[i];
        else
            cout << " " << res[i];
    cout << endl;
    return 0;
}
0