/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
if (fopen(filein.c_str(), "r")){
freopen(filein.c_str(), "r", stdin);
freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
freopen(fileerr.c_str(), "w", stderr);
#endif
}
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main(){
#ifdef hollwo_pelw_local
FAST_IO("input.inp", "output.out", "error.err");
auto start = chrono::steady_clock::now();
#else
FAST_IO("BOI16_cities.inp", "BOI16_cities.out");
#endif
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = chrono::steady_clock::now();
cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
return 0;
}
const int N = 2e5 + 5, K = 5;
#define int int64_t
int n, k, m, node[K];
int64_t dp[1 << K][N];
vector<pair<int, int>> adj[N];
struct dist_t {
int64_t d; int u;
dist_t (int _d = 0, int _u = 0)
: d(_d), u(_u) {}
bool operator < (const dist_t &o) const {
return d > o.d;
}
};
inline void dijkstra(int64_t * dist) {
priority_queue<dist_t> pq;
for (int i = 1; i <= n; i++)
pq.emplace(dist[i], i);
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if (d != dist[u]) continue ;
for (auto [v, w] : adj[u]) {
if (dist[v] > d + w)
pq.emplace(dist[v] = d + w, v);
}
}
}
void Hollwo_Pelw() {
cin >> n >> k >> m;
for (int i = 0; i < k; i++)
cin >> node[i];
for (int u, v, w; m --; )
cin >> u >> v >> w, adj[u].emplace_back(v, w), adj[v].emplace_back(u, w);
memset(dp, 0x3f, sizeof dp);
for (int i = 0; i < k; i++)
dp[1 << i][node[i]] = 0, dijkstra(dp[1 << i]);
for (int mask = 1; mask < 1 << k; mask++) {
if ((mask & -mask) == mask) continue ;
for (int sub = mask; sub; sub = (sub - 1) & mask) {
for (int u = 1; u <= n; u++) for (auto [v, w] : adj[u])
dp[mask][u] = min(dp[mask][u], dp[sub][u] + dp[mask ^ sub][v] + w);
}
dijkstra(dp[mask]);
}
cout << dp[(1 << k) - 1][node[0]];
}
Compilation message
cities.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
cities.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(filein.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(fileout.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
55044 KB |
Output is correct |
2 |
Correct |
20 ms |
54988 KB |
Output is correct |
3 |
Correct |
20 ms |
55116 KB |
Output is correct |
4 |
Correct |
20 ms |
55068 KB |
Output is correct |
5 |
Correct |
20 ms |
55068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
637 ms |
71968 KB |
Output is correct |
2 |
Correct |
609 ms |
71572 KB |
Output is correct |
3 |
Correct |
350 ms |
63968 KB |
Output is correct |
4 |
Correct |
91 ms |
64192 KB |
Output is correct |
5 |
Correct |
293 ms |
71912 KB |
Output is correct |
6 |
Correct |
78 ms |
64160 KB |
Output is correct |
7 |
Correct |
22 ms |
55244 KB |
Output is correct |
8 |
Correct |
22 ms |
55244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
55264 KB |
Output is correct |
2 |
Correct |
24 ms |
55204 KB |
Output is correct |
3 |
Correct |
24 ms |
55116 KB |
Output is correct |
4 |
Correct |
24 ms |
55240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1269 ms |
71972 KB |
Output is correct |
2 |
Correct |
1287 ms |
71524 KB |
Output is correct |
3 |
Correct |
726 ms |
64092 KB |
Output is correct |
4 |
Correct |
785 ms |
69304 KB |
Output is correct |
5 |
Correct |
222 ms |
65624 KB |
Output is correct |
6 |
Correct |
128 ms |
66664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2881 ms |
71860 KB |
Output is correct |
2 |
Correct |
2910 ms |
72016 KB |
Output is correct |
3 |
Correct |
2591 ms |
71592 KB |
Output is correct |
4 |
Correct |
1548 ms |
63992 KB |
Output is correct |
5 |
Correct |
1757 ms |
69436 KB |
Output is correct |
6 |
Correct |
471 ms |
65696 KB |
Output is correct |
7 |
Correct |
239 ms |
66432 KB |
Output is correct |