#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> bool chkmin(T&x,T y){return x<y?x=y,1:0;}
template<typename T> bool chkmax(T&x,T y){return x>y?x=y,1:0;}
int n,k,m,id[100005];
ll dist[100005][1<<5];
vector<pii> g[100005];
int main(){
scanf("%d%d%d",&n,&k,&m);
memset(id,-1,sizeof id);
memset(dist,-1,sizeof dist);
set<pair<ll,pii>> que;
for(int i=1;i<=k;i++){
int x;scanf("%d",&x);
id[x]=i-1;
dist[x][1<<(i-1)]=0;
que.insert(mp(0,mp(x,1<<(i-1))));
}
for(int i=1;i<=n;i++)dist[i][0]=0,que.insert(mp(0,mp(i,0)));
for(int i=1;i<=m;i++){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
g[u].pb(mp(v,w)); g[v].pb(mp(u,w));
}
while(!que.empty()){
auto it=que.begin();
int u=it->second.first;
int mask=it->second.second;
que.erase(it);
for(auto e:g[u]){
int v=e.first,w=e.second;
//int nmask=(mask|(id[v]==-1?0:(1<<id[v])));
for(int s=0;s<(1<<k);s++){
if(dist[v][s]==-1)continue;
int ns=(s|mask);
if(dist[v][ns]==-1||dist[v][ns]>dist[u][mask]+dist[v][s]+w){
if(dist[v][ns]!=-1)que.erase(que.find(mp(dist[v][ns],mp(v,ns))));
dist[v][ns]=dist[u][mask]+dist[v][s]+w;
que.insert(mp(dist[v][ns],mp(v,ns)));
}
}
}
}
ll ans=(ll)1e18;
for(int i=1;i<=n;i++)ans=min(ans,dist[i][(1<<k)-1]);
printf("%lld",ans);
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:23:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf("%d%d%d",&n,&k,&m);
| ~~~~~^~~~~~~~~~~~~~~~~~~
cities.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | int x;scanf("%d",&x);
| ~~~~~^~~~~~~~~
cities.cpp:35:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | int u,v,w;scanf("%d%d%d",&u,&v,&w);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
27988 KB |
Output is correct |
2 |
Correct |
12 ms |
28088 KB |
Output is correct |
3 |
Correct |
13 ms |
27988 KB |
Output is correct |
4 |
Correct |
13 ms |
28104 KB |
Output is correct |
5 |
Correct |
12 ms |
28116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2981 ms |
60576 KB |
Output is correct |
2 |
Correct |
2368 ms |
56172 KB |
Output is correct |
3 |
Correct |
581 ms |
37480 KB |
Output is correct |
4 |
Correct |
193 ms |
32532 KB |
Output is correct |
5 |
Correct |
805 ms |
42840 KB |
Output is correct |
6 |
Correct |
102 ms |
32584 KB |
Output is correct |
7 |
Correct |
18 ms |
28320 KB |
Output is correct |
8 |
Correct |
17 ms |
28172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
28712 KB |
Output is correct |
2 |
Correct |
27 ms |
28628 KB |
Output is correct |
3 |
Correct |
18 ms |
28244 KB |
Output is correct |
4 |
Correct |
24 ms |
28388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6050 ms |
102932 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6048 ms |
168648 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |