Từ Trường THPT chuyên Hoàng Lê Kha, Tây Ninh
Thông tin
include <bits/stdc++.h>
using namespace std; vector<int> dist(100001,1e9); int m,n,s,t; vector<pair<int,int>> adj[100001]; priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> q; void dijkstra (int s) { dist[s] = 0; q.push({0, s}); while(!q.empty()) { int du=q.top().first; int u=q.top().second; q.pop(); if(du>dist[u]) continue; for(auto g:adj[u]) { int v=g.first; int w=g.second; if(dist[v]>dist[u]+w) { dist[v]=dist[u]+w; q.push({dist[v],v}); } } }
}
int main () { iosbase::syncwithstdio(false); cin.tie(NULL); cin>>m>>n; for(int i=1; i<=n; i++) { int x,y,w; cin>>x>>y>>w; adj[x].pushback({y,w}); adj[y].push_back({x,w}); } int k; cin>>k; int s[k+1]; for(int i=1; i<=k; i++) { cin>>s[i]; } cin>>t; dijkstra(t); int minadj=1e9; for(int i=1; i<=k; i++) { if(dist[s[i]]<minadj) { minadj=dist[s[i]]; } } if(minadj!=1e9) cout<<minadj; else cout<<-1; } /* 6 7 1 2 4 2 3 2 3 4 7 4 5 1 5 6 3 2 6 10 1 6 20 2 1 5 4 */