#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "262144"
#define pll pair<ll, ll>
#define pi pair<ll, pll>
#define fi first
#define se second
using namespace std;
const ll mod = 1e15;
const ll N = 2e5+5;
const int base = 313;
ll n, m, t, k, T, ans, tong, a[N], dp[N][(1<<5)];
vector<pll> adj[N];
vector<ll> kq;
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void sol()
{
cin >> n >> k >> m;
for(int i = 1; i <= k; i ++)
{
ll x;
cin >> x;
--x;
kq.pb(x);
}
while(m -- > 0)
{
ll x, y, w;
cin >> x >> y >> w;
--x;
--y;
adj[x].pb({w, y});
adj[y].pb({w, x});
}
for(int i = 0; i < n; i ++)for(int j = 1; j < (1<<k); j ++)dp[i][j] = mod;
for(int i = 0; i < k; i ++)dp[kq[i]][(1<<i)] = 0;
for(int mask = 0; mask < (1<<k); mask ++)
{
priority_queue< pll, vector<pll>, greater<pll> > pq;
for(int i = 0; i < n; i ++)
{
for(int j = i; j >= 0; j = (j - 1) & i)
{
dp[i][mask] = min(dp[i][mask], dp[i][j] + dp[i][mask^j]);
if(!j)break;
}
if(dp[i][mask] < mod)pq.push({dp[i][mask], i});
}
while(!pq.empty())
{
pll u = pq.top();
pq.pop();
if(u.fi != dp[u.se][mask])continue;
//cout << u.se <<" "<<mask <<" "<<u.fi << '\n';
if(mask == (1<<k)-1)
{
cout << u.fi;
return;
}
for(pll v : adj[u.se])
{
if(dp[v.se][mask] > u.fi + v.fi)
{
dp[v.se][mask] = u.fi + v.fi;
pq.push({dp[v.se][mask], v.se});
}
}
}
}
}
int main()
{
if(fopen(task".in", "r"))
{
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
4 5 2
10 9 5 2
6 4 20 15
9 7 10 9
-1 -1 16 11
1 2 3
2 3 3
1 4 1
4 3 1
3 1 1
*/
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:86:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen(task".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:87:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1700 ms |
48188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
5444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3404 ms |
48312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6016 ms |
48240 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |