#include <bits/stdc++.h>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "City"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
/// dp[u][x] la trong so cua cay nho nhat sao cho u va cac dinh thuoc x lien thong
int n, k, m;
int spe[maxn];
ll dp[(1 << 5) + 1][maxn];
vector<pair<int, int>> edges[maxn];
void DJK(ll *dist) {
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>> > q;
fi(i, 1, n) q.push({dist[i], i});
while(!q.empty()) {
int u = q.top().second, g = q.top().first;
q.pop();
if(g > dist[u]) continue;
for(auto [v, w] : edges[u]) if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
q.push({dist[v], v});
}
}
}
void solve()
{
cin >> n >> k >> m;
fi(i, 1, k) cin >> spe[i];
fi(i, 1, m) {
int x, y, w; cin >> x >> y >> w;
edges[x].push_back({y, w}), edges[y].push_back({x, w});
}
memset(dp, 60, sizeof dp);
fi(i, 1, k) {
int u = spe[i];
dp[(1 << (i - 1))][u] = 0;
}
fi(x, 1, (1 << k) - 1) {
fi(u, 1, n) for(int y = x; y > 0; y = (y - 1) & x) {
dp[x][u] = min(dp[x][u], dp[x ^ y][u] + dp[y][u]); /// gop 2 cau truc cay co chung goc la u
}
/// tu 1 cau truc cay toi uu loang sang cac dinh ko thuoc cay nay
DJK(dp[x]);
}
ll ans = 1e18;
fi(i, 1, n) ans = min(ans, dp[(1 << k) - 1][i]);
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:91:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
56668 KB |
Output is correct |
2 |
Correct |
23 ms |
56668 KB |
Output is correct |
3 |
Correct |
24 ms |
56660 KB |
Output is correct |
4 |
Correct |
24 ms |
56620 KB |
Output is correct |
5 |
Correct |
25 ms |
56664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
361 ms |
74048 KB |
Output is correct |
2 |
Correct |
381 ms |
73800 KB |
Output is correct |
3 |
Correct |
225 ms |
65684 KB |
Output is correct |
4 |
Correct |
61 ms |
64932 KB |
Output is correct |
5 |
Correct |
211 ms |
73800 KB |
Output is correct |
6 |
Correct |
59 ms |
64848 KB |
Output is correct |
7 |
Correct |
26 ms |
56924 KB |
Output is correct |
8 |
Correct |
31 ms |
56740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
56752 KB |
Output is correct |
2 |
Correct |
29 ms |
56916 KB |
Output is correct |
3 |
Correct |
30 ms |
56668 KB |
Output is correct |
4 |
Correct |
26 ms |
56760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
698 ms |
73792 KB |
Output is correct |
2 |
Correct |
689 ms |
73736 KB |
Output is correct |
3 |
Correct |
435 ms |
65680 KB |
Output is correct |
4 |
Correct |
392 ms |
70360 KB |
Output is correct |
5 |
Correct |
146 ms |
66868 KB |
Output is correct |
6 |
Correct |
69 ms |
66716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1336 ms |
73720 KB |
Output is correct |
2 |
Correct |
1400 ms |
73752 KB |
Output is correct |
3 |
Correct |
1330 ms |
73748 KB |
Output is correct |
4 |
Correct |
944 ms |
65676 KB |
Output is correct |
5 |
Correct |
713 ms |
70348 KB |
Output is correct |
6 |
Correct |
193 ms |
66864 KB |
Output is correct |
7 |
Correct |
74 ms |
66604 KB |
Output is correct |