#include <bits/stdc++.h>
using namespace std;
#define task "main"
#define F first
#define S second
#define ii pair<int, int>
#define il pair<int, long long>
#define li pair<long long, int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
const int MAX_N = (int)1e5 + 5;
const long long INF = (long long)1e18 + 1408;
int nNode, nEdge, k;
int special[6];
vector<ii> adj[MAX_N];
long long dp[MAX_N][1 << 5];
void solve() {
cin >> nNode >> k >> nEdge;
FOR(i, 0, k - 1) cin >> special[i];
FOR(i, 1, nEdge) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
memset(dp, 0x3f, sizeof(dp));
FOR(i, 0, k - 1) dp[special[i]][1 << i] = 0;
priority_queue<li, vector<li>, greater<li>> pq;
FOR(mask, 1, (1 << k) - 1) {
FOR(u, 1, nNode)
for (int submask = (mask - 1) & mask; submask; submask = (submask - 1) & mask)
minimize(dp[u][mask], dp[u][submask] + dp[u][mask ^ submask]);
FOR(u, 1, nNode) if (dp[u][mask] < INF)
pq.push({dp[u][mask], u});
while (!pq.empty()) {
li tmp = pq.top();
pq.pop();
int u = tmp.S;
if (dp[u][mask] < tmp.F) continue;
for (ii e : adj[u]) {
int v = e.F, w = e.S;
if (minimize(dp[v][mask], dp[u][mask] + w))
pq.push({dp[v][mask], v});
}
}
}
cout << dp[special[0]][(1 << k) - 1];
}
int32_t main() {
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
bool multitest = 0;
int numTest = 1;
if (multitest) cin >> numTest;
while (numTest--) {
solve();
}
return 0;
}
/* Lak lu theo dieu nhac!!!! */
Compilation message (stderr)
cities.cpp: In function 'int32_t main()':
cities.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |