結果
| 問題 |
No.1814 Uribo Road (Easy)
|
| ユーザー |
MasKoaTS
|
| 提出日時 | 2021-11-05 12:02:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 403 ms / 2,000 ms |
| コード長 | 1,752 bytes |
| コンパイル時間 | 1,495 ms |
| コンパイル使用メモリ | 92,832 KB |
| 最終ジャッジ日時 | 2025-01-25 11:49:44 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 44 |
ソースコード
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define rep(i, l, n) for (int i = (l); i < (n); i++)
#define all(x) x.begin(), x.end()
using namespace std;
using Pair = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T> >;
const int inf = 2000000000;
VV<int> dijkstra(int n, VV<Pair>&route) {
VV<int> dist(n, V<int>(n, -1));
rep(i, 0, n) {
priority_queue<Pair, V<Pair>, greater<Pair> > pq;
pq.push({ 0,i });
int cnt = 0;
while (pq.size() && cnt < n) {
Pair p = pq.top(); pq.pop();
int d = p.first; int v = p.second;
if (dist[i][v] == -1) {
cnt++;
dist[i][v] = d;
rep(i, 0, route[v].size()) {
Pair p = { d + route[v][i].second,route[v][i].first };
pq.push(p);
}
}
}
}
return dist;
}
int main(void) {
int N, M, K; cin >> N >> M >> K;
V<int> R(K);
rep(i, 0, K) {
cin >> R[i]; R[i] -= 1;
}
sort(all(R));
VV<int> edge(M, V<int>(3));
VV<Pair> route(N, V<Pair>(0));
rep(i, 0, M) {
rep(j, 0, 3) {
cin >> edge[i][j];
}
edge[i][0] -= 1; edge[i][1] -= 1;
route[edge[i][0]].push_back({ edge[i][1],edge[i][2] });
route[edge[i][1]].push_back({ edge[i][0],edge[i][2] });
}
int ans = inf;
int rsum = 0;
rep(i, 0, R.size()) {
rsum += edge[R[i]][2];
}
VV<int> dist = dijkstra(N, route);
do {
rep(bit, 0, 1 << K) {
int s = rsum;
V<int> path(1);
rep(i, 0, K) {
int b = (bit >> i) & 1;
path.push_back(edge[R[i]][b]);
path.push_back(edge[R[i]][b ^ 1]);
}
path.push_back(N - 1);
for (int i = 0; i < path.size() - 1; i += 2) {
s += dist[path[i]][path[i + 1]];
}
if (ans > s) {
ans = s;
}
}
} while (next_permutation(all(R)));
cout << ans << endl;
return 0;
}
MasKoaTS