# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
821090 | Alihan_8 | Factories (JOI14_factories) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
#define ln '\n'
#define int long long
template <class _T>
bool chmin(_T &x, const _T &y){
bool flag = false;
if ( x > y ){
x = y; flag |= true;
}
return flag;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag = false;
if ( x < y ){
x = y; flag |= true;
}
return flag;
}
const int inf = 1e16 + 1;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q; cin >> n >> q;
vector <pair<int,int>> g[n];
for ( int i = 0; i + 1 < n; i++ ){
int x, y, w; cin >> x >> y >> w;
g[x].pb({y, w});
g[y].pb({x, w});
}
while ( q-- ){
int X, Y; cin >> X >> Y;
priority_queue <pair<int,int>> q;
vector <int> dis(n, inf);
for ( int i = 0; i < X; i++ ){
int u; cin >> u;
dis[u] = 0;
q.push({0, u});
}
while ( !q.empty() ){
auto [val, u] = q.top();
q.pop(); val *= -1;
if ( dis[u] != val ){
continue;
}
for ( auto [v, w]: g[u] ){
if ( chmin(dis[v], dis[u] + w) ){
q.push({-dis[v], v});
}
}
}
int ans = inf;
for ( int i = 0; i < Y; i++ ){
int u; cin >> u;
chmin(ans, dis[u]);
}
cout << ans << ln;
}
cout << '\n';
}