#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[100005];
priority_queue<pair<int, int> > pq;
long long dp[32][100005];
bool visited[100005];
int a[5];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, p;
cin >> n >> p >> m;
for (int i=0; i<p; i++)
cin >> a[i];
for (int i=0; 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 i=0; i<(1<<p); i++)
for (int j=1; j<=n; j++)
dp[i][j]=1e18;
for (int i=0; i<p; i++)
dp[1<<i][a[i]]=0;
for (int i=1; i<(1<<p); i++)
{
for (int j=0; j<p; j++)
if (i&(1<<j))
for (int k=1; k<=n; k++)
dp[i][k]=min(dp[i][k], dp[i^(1<<j)][k]+dp[1<<j][k]);
for (int j=1; j<=n; j++)
if (dp[i][j]<1e18)
pq.push({-dp[i][j], j});
while (!pq.empty())
{
int u=pq.top().second;
pq.pop();
if (visited[u])
continue;
for (int j=0; j<adj[u].size(); j++)
{
int v=adj[u][j].first, w=adj[u][j].second;
if (dp[i][u]+w<dp[i][v])
{
dp[i][v]=dp[i][u]+w;
pq.push({-dp[i][v], v});
}
}
}
}
long long ans=1e18;
for (int i=1; i<=n; i++)
ans=min(ans, dp[(1<<p)-1][i]);
cout << ans;
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:44:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int j=0; j<adj[u].size(); j++)
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
998 ms |
21524 KB |
Output is correct |
2 |
Correct |
428 ms |
21172 KB |
Output is correct |
3 |
Correct |
656 ms |
15320 KB |
Output is correct |
4 |
Correct |
75 ms |
10700 KB |
Output is correct |
5 |
Correct |
516 ms |
17480 KB |
Output is correct |
6 |
Correct |
63 ms |
10692 KB |
Output is correct |
7 |
Correct |
4 ms |
2900 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
2944 KB |
Output is correct |
2 |
Correct |
6 ms |
2900 KB |
Output is correct |
3 |
Correct |
6 ms |
2900 KB |
Output is correct |
4 |
Correct |
4 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1575 ms |
27792 KB |
Output is correct |
2 |
Correct |
833 ms |
27444 KB |
Output is correct |
3 |
Correct |
1404 ms |
21592 KB |
Output is correct |
4 |
Correct |
688 ms |
20028 KB |
Output is correct |
5 |
Correct |
154 ms |
12892 KB |
Output is correct |
6 |
Correct |
77 ms |
12532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2900 ms |
40468 KB |
Output is correct |
2 |
Correct |
2880 ms |
40324 KB |
Output is correct |
3 |
Correct |
1837 ms |
39996 KB |
Output is correct |
4 |
Correct |
3134 ms |
34132 KB |
Output is correct |
5 |
Correct |
1220 ms |
26520 KB |
Output is correct |
6 |
Correct |
232 ms |
14224 KB |
Output is correct |
7 |
Correct |
101 ms |
12612 KB |
Output is correct |