1 solutions
-
0
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> pii; const int N=802,INF=0x3f3f3f3f; vector<pii> h[N]; int a[N],d[N][N]; void dij(int root,int n) { priority_queue<pii, vector<pii>, greater<pii>> q; vector<char> vis(n+1,0); q.push({0,root}); while(!q.empty()) { auto [_d,p]=q.top(); q.pop(); if(vis[p])continue; vis[p]=1; for(auto [it,w]:h[p]) if(!vis[it]&&d[root][it]>d[root][p]+w) { d[root][it]=d[root][p]+w; q.push({d[root][it],it}); } } } signed main(){ ios::sync_with_stdio(false); cin.tie(nullptr); memset(d,0x3f,sizeof d); for(int i=0;i<N;i++)d[i][i]=0; int num,n,m; cin>>num>>n>>m; for(int i=1;i<=num;i++) { int c; cin>>c; a[c]++; } while(m--) { int u,v,w; cin>>u>>v>>w; h[u].push_back({v,w}); h[v].push_back({u,w}); } for(int i=1;i<=n;i++)dij(i,n); int ans=INF; for(int i=1;i<=n;i++) { int temp=0; bool val=1; for(int j=1;j<=n;j++) { if(a[j]&&d[j][i]==INF){val=0;break;} temp+=a[j]*d[j][i]; } if(val) ans=min(ans,temp); } cout<<ans; return 0; }
Information
- ID
- 6885
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 9
- Tags
- (None)
- # Submissions
- 14
- Accepted
- 4
- Uploaded By