#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10;
int n,m,k;
long long f[M][1<<5];
vector<pair<int,int>> ke[M];
priority_queue<tuple<long long ,int ,int >,vector<tuple<long long ,int ,int > > ,greater<tuple<long long ,int ,int > > > h;
vector<int> submask[1<<5];
void dij()
{
while(!h.empty())
{
auto [val,mask,u]=h.top();h.pop();
if(f[u][mask]<val) continue;
for(pair<int,int> v:ke[u])
{
for(int i:submask[mask])
{
if(val+f[v.first][i^mask]+v.second < f[v.first][i])
{
f[v.first][i] = val+f[v.first][i^mask]+v.second;
h.push({f[v.first][i],i,v.first});
}
}
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task "1"
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k >> m;
for(int i=0;i<(1<<k);i++)
{
for(int j=(1 << k) - 1;j >= 0;j--) if((i&j)==i) submask[i].push_back(j);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<(1<<k);j++) f[i][j]=1e18;
}
for(int i=0;i<k;i++)
{
int x;
cin >> x;
h.push({0,1<<i,x});
f[x][1<<i]=0;
}
for(int i=1;i<=m;i++)
{
int u,v;
long long w;
cin >> u >> v >> w;
ke[u].push_back({v,w});
ke[v].push_back({u,w});
}
dij();
long long kq=1e18;
for(int i=1;i<=n;i++) kq=min(kq,f[i][(1<<k)-1]);
cout << kq;
}
컴파일 시 표준 에러 (stderr) 메시지
cities.cpp: In function 'int32_t main()':
cities.cpp:39:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:40:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | 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... |