This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define plii pair<long long, pair<int, int>>
const int maxN = 1e4 + 5; const long long inf = 1e18;
int n, m, k, T, s[maxN], pos[maxN];
long long dis[1000][maxN];
pair<int, int> lt[1000][maxN], rt[1000][maxN];
vector<pair<int, int>> adj[maxN];
vector<int> save[maxN];
void dijkstra() {
priority_queue<plii, vector<plii>, greater<plii>> pq;
for (int i = 1; i <= k; i++) {
pq.push({0, {s[i], (1 << (i - 1))}});
dis[(1 << (i - 1))][s[i]] = 0;
}
while (pq.size() > 0) {
auto [len, rnd] = pq.top(); pq.pop();
int u = rnd.first, mask = rnd.second;
if (dis[mask][u] != len) continue;
for (auto it: adj[u]) {
int v = it.first, w = it.second;
for (auto sub: save[mask])
if (dis[sub][v] > dis[sub ^ mask][v] + len + w) {
dis[sub][v] = dis[sub ^ mask][v] + len + w;
lt[sub][v] = {sub ^ mask, v}, rt[sub][v] = {mask, u};
pq.push({dis[sub][v], {v, sub}});
}
}
}
}
struct tmp {
int mask, u, v;
} order[3000005];
int step = 0;
void backtrack(int u, int mask) {
if (lt[mask][u].first != 0) {
if (lt[mask][u].second != u) order[++step] = {lt[mask][u].first, lt[mask][u].second, u};
backtrack(lt[mask][u].second, lt[mask][u].first);
}
if (rt[mask][u].first != 0) {
if (rt[mask][u].second != u) order[++step] = {rt[mask][u].first, rt[mask][u].second, u};
backtrack(rt[mask][u].second, rt[mask][u].first);
}
}
string convert(int mask) {
string s = "";
while (mask > 0) {
s = s + char(mask % 2 + '0');
mask = mask / 2;
}
while (s.size() < k) s += "0";
return s;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> k >> T;
for (int i = 1; i <= k; i++) {
cin >> s[i];
pos[s[i]] = 1;
}
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (int mask = 1; mask < (1 << k); mask++)
for (int S = mask; S > 0; S = (mask & (S - 1))) {
save[S].push_back(mask);
}
for (int i = 1; i <= n; i++) {
dis[0][i] = 0;
for (int mask = 1; mask < (1 << k); mask++)
dis[mask][i] = inf;
}
dijkstra();
cout << dis[(1 << k) - 1][T] << '\n';
//backtrack(T, (1 << k) - 1);
/*cout << step << '\n';
for (int i = step; i >= 1; i--) {
auto [mask, u, v] = order[i];
cout << convert(mask) << ' ' << u << ' ' << v << '\n';
}*/
return 0;
}
Compilation message (stderr)
cities.cpp: In function 'std::string convert(int)':
cities.cpp:61:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
61 | while (s.size() < k) s += "0";
| ~~~~~~~~~^~~
# | 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... |