#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
const int nmax=100005;
vector< pair<int,long long> > v[nmax];
long long d[nmax][32];
bool E[nmax][32];
int norm[nmax],imp[10];
long long di,newd,ans;
int x,n,m,nod,k,i,j,y,z,stare,stare_noua;
struct node
{
long long D;
int node,state;
};
struct cmp
{
bool operator()(node unu,node doi)
{
return unu.D>doi.D;
}
};
priority_queue <node,vector<node>,cmp > pq;
void dij()
{
while(!pq.empty())
{
x=pq.top().node;di=pq.top().D;stare=pq.top().state;
pq.pop();
if(!E[x][stare])
{
E[x][stare]=1;
for(i=0;i<v[x].size();i++)
{
nod=v[x][i].first;newd=v[x][i].second+di;
if(newd<d[nod][stare])
{
d[nod][stare]=newd;
pq.push({newd,nod,stare});
}
}
nod=x;
for(i=1;i<(1<<k);i++)
{
stare_noua=(stare|i);newd=1LL*d[x][stare]+d[x][i];
if(newd<d[x][stare_noua])
{
d[x][stare_noua]=newd;
pq.push({newd,x,stare_noua});
}
}
}
}
}
int main()
{
// freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n>>k>>m;
for(i=1;i<=k;i++)
{
cin>>imp[i];
norm[imp[i]]=i;
}
for(i=1;i<=m;i++)
{
cin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
for(i=1;i<=n;i++)
for(j=1;j<(1<<k);j++)
d[i][j]=1LL*1e17;
for(i=1;i<=k;i++)
{
d[imp[i]][(1<<(i-1))]=0;
pq.push({0,imp[i],(1<<(i-1))});
}
dij();
ans=LLONG_MAX;
for(i=1;i<=n;i++)
ans=1LL*min(ans,d[i][(1<<k)-1]);
cout<<ans;
return 0;
}
Compilation message
cities.cpp: In function 'void dij()':
cities.cpp:37:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2928 KB |
Output is correct |
3 |
Correct |
5 ms |
2928 KB |
Output is correct |
4 |
Correct |
5 ms |
2928 KB |
Output is correct |
5 |
Correct |
5 ms |
2968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1206 ms |
62396 KB |
Output is correct |
2 |
Correct |
972 ms |
62396 KB |
Output is correct |
3 |
Correct |
627 ms |
62396 KB |
Output is correct |
4 |
Correct |
136 ms |
62396 KB |
Output is correct |
5 |
Correct |
484 ms |
64284 KB |
Output is correct |
6 |
Correct |
104 ms |
64284 KB |
Output is correct |
7 |
Correct |
8 ms |
64284 KB |
Output is correct |
8 |
Correct |
7 ms |
64284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
64284 KB |
Output is correct |
2 |
Correct |
15 ms |
64284 KB |
Output is correct |
3 |
Correct |
10 ms |
64284 KB |
Output is correct |
4 |
Correct |
11 ms |
64284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3080 ms |
100824 KB |
Output is correct |
2 |
Correct |
2646 ms |
104328 KB |
Output is correct |
3 |
Correct |
1830 ms |
104328 KB |
Output is correct |
4 |
Correct |
1738 ms |
104328 KB |
Output is correct |
5 |
Correct |
390 ms |
104328 KB |
Output is correct |
6 |
Correct |
158 ms |
104328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6098 ms |
219952 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |