#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
int a[6],n,m,k;
ll dp[33][200001];
vector<pii> adj[200001];
void dij(ll *dis) {
priority_queue<pii, vector<pii>, greater<pii>> q;
bool vis[200001];
memset(vis,0,sizeof(vis));
for (int i=1; i<=n; ++i) q.push(pii(dis[i],i));
while (!q.empty()) {
ll node=q.top().second;
q.pop();
if (vis[node]) continue;
vis[node]=true;
for (auto s : adj[node]) {
ll des=s.first, w=s.second;
if (dis[des]>dis[node]+w) {
dis[des]=dis[node]+w;
q.push(pii(dis[des],des));
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
for (int i=0; i<32; ++i) for (int j=1; j<=200000; ++j) dp[i][j]=1e18;
cin>>n>>k>>m;
for (int i=0; i<k; ++i) {
cin>>a[i];
dp[1<<i][a[i]]=0;
}
for (int i=0; i<m; ++i) {
int a,b,w;
cin>>a>>b>>w;
adj[a].push_back(pii(b,w));
adj[b].push_back(pii(a,w));
}
for (int bit=1; bit<(1<<k); ++bit) {
for (int j=0; j<k; ++j) {
if (!bit&(1<<j)) continue;
for (int i=1; i<=n; ++i) dp[bit][i]=min(dp[bit][i],dp[j][i]+dp[bit^j][i]);
}
dij(dp[bit]);
}
ll ans=LLONG_MAX;
for (int i=1; i<=n; ++i) ans=min(ans,dp[(1<<k)-1][i]);
cout<<ans;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:49:17: warning: suggest parentheses around operand of '!' or change '&' to '&&' or '!' to '~' [-Wparentheses]
49 | if (!bit&(1<<j)) continue;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
55252 KB |
Output is correct |
2 |
Correct |
24 ms |
55284 KB |
Output is correct |
3 |
Correct |
22 ms |
55316 KB |
Output is correct |
4 |
Incorrect |
25 ms |
55244 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
545 ms |
74344 KB |
Output is correct |
2 |
Correct |
517 ms |
74768 KB |
Output is correct |
3 |
Correct |
350 ms |
66284 KB |
Output is correct |
4 |
Correct |
85 ms |
67296 KB |
Output is correct |
5 |
Correct |
318 ms |
75124 KB |
Output is correct |
6 |
Correct |
105 ms |
67264 KB |
Output is correct |
7 |
Correct |
27 ms |
55432 KB |
Output is correct |
8 |
Correct |
25 ms |
55428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
55416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
872 ms |
74424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1673 ms |
74332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |