This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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<<6)];
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 = mask; j > 0; j = (j - 1) & mask)
{
dp[i][mask] = min(dp[i][mask], dp[i][j] + dp[i][mask^j]);
}
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 (stderr)
cities.cpp: In function 'int main()':
cities.cpp:85:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen(task".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
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".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |