# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
84252 | ToMoClone | Cities (BOI16_cities) | C++14 | 5466 ms | 137028 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
4 3 6
1 3 4
1 2 4
1 3 9
1 4 6
2 3 2
2 4 5
3 4 8
*/
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
struct State {
int node; int64_t d; int mask;
State(int _node = 0, int64_t _d = 1e15, int _mask = 0) :
node(_node), d(_d), mask(_mask) {}
bool operator<(const State &x) const {
return d > x.d;
}
};
const int N = 100005;
int n, k, m;
int64_t d[N][32];
queue<State> q;
vector<pair<int, int> > G[N];
int32_t main(){
memset(d, 127, sizeof d);
scanf("%d%d%d", &n, &k, &m);
for(int i = 0; i < k; ++ i) {
int a; scanf("%d", &a);
d[a][1 << i] = 0, q.push(State(a, 0, 1 << i));
}
for(int i = 1; i <= m; ++ i) {
int u, v, c; scanf("%d%d%d", &u, &v, &c);
G[u].push_back(make_pair(c, v));
G[v].push_back(make_pair(c, u));
}
while(!q.empty()) {
auto cur = q.front(); q.pop();
if(d[cur.node][cur.mask] != cur.d) continue;
for(int i = 0; i < k; ++ i) if((cur.mask & (1 << i)) == 0) for(auto u : G[cur.node]) {
if(d[cur.node][(1 << i) | cur.mask] > cur.d + d[u.second][1 << i] + u.first) {
d[cur.node][(1 << i) | cur.mask] = cur.d + d[u.second][1 << i] + u.first;
q.push(State(cur.node, d[cur.node][(1 << i) | cur.mask], (1 << i) | cur.mask));
}
if(d[u.second][(1 << i) | cur.mask] > cur.d + d[u.second][1 << i] + u.first) {
d[u.second][(1 << i) | cur.mask] = cur.d + d[u.second][1 << i] + u.first;
q.push(State(u.second, d[u.second][(1 << i) | cur.mask], (1 << i) | cur.mask));
}
}
for(auto u : G[cur.node])
if(d[u.second][cur.mask] > cur.d + u.first) {
d[u.second][cur.mask] = cur.d + u.first;
q.push(State(u.second, d[u.second][cur.mask], cur.mask));
}
}
int64_t Ans = 1e15;
for(int i = 1; i <= n; ++ i) Ans = min(Ans, d[i][(1 << k) - 1]);
cout << Ans << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |