結果
| 問題 |
No.1812 Uribo Road
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2022-01-14 22:55:29 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 87 ms / 5,000 ms |
| コード長 | 3,989 bytes |
| コンパイル時間 | 2,862 ms |
| コンパイル使用メモリ | 216,668 KB |
| 最終ジャッジ日時 | 2025-01-27 12:04:19 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 30 |
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
template <typename CostType>
struct Edge {
int src, dst; CostType cost;
Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
inline bool operator<(const Edge &x) const {
return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
}
inline bool operator<=(const Edge &x) const { return !(x < *this); }
inline bool operator>(const Edge &x) const { return x < *this; }
inline bool operator>=(const Edge &x) const { return !(*this < x); }
};
template <typename CostType>
struct Dijkstra {
const CostType inf;
Dijkstra(const std::vector<std::vector<Edge<CostType>>> &graph,
const CostType inf = std::numeric_limits<CostType>::max())
: graph(graph), inf(inf) {}
std::vector<CostType> build(int s) {
is_built = true;
int n = graph.size();
std::vector<CostType> dist(n, inf);
dist[s] = 0;
prev.assign(n, -1);
using Pci = std::pair<CostType, int>;
std::priority_queue<Pci, std::vector<Pci>, std::greater<Pci>> que;
que.emplace(0, s);
while (!que.empty()) {
CostType cost; int ver; std::tie(cost, ver) = que.top(); que.pop();
if (dist[ver] < cost) continue;
for (const Edge<CostType> &e : graph[ver]) {
if (dist[e.dst] > dist[ver] + e.cost) {
dist[e.dst] = dist[ver] + e.cost;
prev[e.dst] = ver;
que.emplace(dist[e.dst], e.dst);
}
}
}
return dist;
}
std::vector<int> build_path(int t) const {
assert(is_built);
std::vector<int> res;
for (; t != -1; t = prev[t]) res.emplace_back(t);
std::reverse(res.begin(), res.end());
return res;
}
private:
bool is_built = false;
std::vector<std::vector<Edge<CostType>>> graph;
std::vector<int> prev;
};
int main() {
int n, m, k; cin >> n >> m >> k;
vector<int> r(k); REP(i, k) cin >> r[i], --r[i];
vector<vector<Edge<int>>> graph(n);
vector<int> a(k), b(k), c(k), nodes{0, n - 1};
REP(i, m) {
int ai, bi, ci; cin >> ai >> bi >> ci; --ai; --bi;
graph[ai].emplace_back(ai, bi, ci);
graph[bi].emplace_back(bi, ai, ci);
REP(j, k) {
if (r[j] == i) {
a[j] = ai;
b[j] = bi;
c[j] = ci;
nodes.emplace_back(ai);
nodes.emplace_back(bi);
}
}
}
sort(ALL(nodes));
nodes.erase(unique(ALL(nodes)), nodes.end());
const int x = nodes.size();
vector dist(x, vector(x, 0));
Dijkstra dijkstra(graph);
REP(i, x) {
const vector<int> d = dijkstra.build(nodes[i]);
REP(j, x) dist[i][j] = d[nodes[j]];
}
vector dp(1 << k, vector(x, INF));
REP(i, x) dp[0][i] = dist[0][i];
REP(i, 1 << k) {
REP(l, k) {
if (i >> l & 1) continue;
const int p = lower_bound(ALL(nodes), a[l]) - nodes.begin();
const int q = lower_bound(ALL(nodes), b[l]) - nodes.begin();
REP(j, x) {
chmin(dp[i | (1 << l)][q], dp[i][j] + dist[j][p] + c[l]);
chmin(dp[i | (1 << l)][p], dp[i][j] + dist[j][q] + c[l]);
}
}
}
int ans = INF;
REP(i, x) chmin(ans, dp[(1 << k) - 1][i] + dist[i][x - 1]);
cout << ans << '\n';
return 0;
}
emthrm