#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > adj[100005];
priority_queue<pair<long long, long long> > pq;
long long dp[32][100005], a[5];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m, p;
cin >> n >> p >> m;
for (long long i=0; i<p; i++)
cin >> a[i];
for (long long i=0; i<m; i++)
{
long long u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (long long i=0; i<(1<<p); i++)
for (long long j=1; j<=n; j++)
dp[i][j]=1e18;
for (long long i=0; i<p; i++)
dp[1<<i][a[i]]=0;
for (long long i=1; i<(1<<p); i++)
{
for (long long j=0; j<p; j++)
if (i&(1<<j))
for (long long k=1; k<=n; k++)
dp[i][k]=min(dp[i][k], dp[i^(1<<j)][k]+dp[1<<j][k]);
for (long long j=1; j<=n; j++)
if (dp[i][j]<1e18)
pq.push({-dp[i][j], j});
while (!pq.empty())
{
long long d=pq.top().first, u=pq.top().second;
pq.pop();
if (dp[i][u]!=-d)
continue;
for (long long j=0; j<adj[u].size(); j++)
{
long long 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 (long long 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:42:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (long long j=0; j<adj[u].size(); j++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
451 ms |
23460 KB |
Output is correct |
2 |
Correct |
439 ms |
22636 KB |
Output is correct |
3 |
Correct |
231 ms |
16336 KB |
Output is correct |
4 |
Correct |
57 ms |
11696 KB |
Output is correct |
5 |
Correct |
240 ms |
20440 KB |
Output is correct |
6 |
Correct |
74 ms |
12328 KB |
Output is correct |
7 |
Correct |
4 ms |
2936 KB |
Output is correct |
8 |
Correct |
3 ms |
2812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3048 KB |
Output is correct |
2 |
Correct |
6 ms |
3044 KB |
Output is correct |
3 |
Correct |
4 ms |
2976 KB |
Output is correct |
4 |
Correct |
4 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
919 ms |
29920 KB |
Output is correct |
2 |
Correct |
861 ms |
29480 KB |
Output is correct |
3 |
Correct |
607 ms |
23228 KB |
Output is correct |
4 |
Correct |
540 ms |
22408 KB |
Output is correct |
5 |
Correct |
155 ms |
14192 KB |
Output is correct |
6 |
Correct |
68 ms |
14616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1773 ms |
42232 KB |
Output is correct |
2 |
Correct |
1858 ms |
42860 KB |
Output is correct |
3 |
Correct |
1731 ms |
42000 KB |
Output is correct |
4 |
Correct |
1325 ms |
35796 KB |
Output is correct |
5 |
Correct |
996 ms |
28736 KB |
Output is correct |
6 |
Correct |
237 ms |
15644 KB |
Output is correct |
7 |
Correct |
107 ms |
15032 KB |
Output is correct |