結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
小指が強い人
|
| 提出日時 | 2016-01-11 14:03:32 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,146 bytes |
| コンパイル時間 | 995 ms |
| コンパイル使用メモリ | 90,000 KB |
| 実行使用メモリ | 10,524 KB |
| 最終ジャッジ日時 | 2024-09-19 18:46:33 |
| 合計ジャッジ時間 | 7,804 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | WA * 3 TLE * 1 -- * 22 |
ソースコード
#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;
}
小指が強い人