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;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const ll inf = 1e18;
const int maxn = 1e6 + 5;
void spa(vector<vector<pair<ll,int>>> g, vector<ll>& dist, int src) {
int n = g.size();
dist.resize(n);
for (int i=0; i<n; i++) {
dist[i] = inf;
}
dist[src] = 0;
priority_queue<pair<ll,int>> pq;
pq.emplace(0,src);
while (!pq.empty()) {
auto cur = pq.top();
pq.pop();
ll wei = -cur.first;
int at = cur.second;
if (wei > dist[at]) continue;
for (auto ed: g[at]) {
ll d = ed.first;
int to = ed.second;
if (wei+d < dist[to]) {
dist[to] = wei+d;
pq.emplace(-dist[to], to);
}
}
}
}
int n,m,k;
vector<vector<pair<ll,int>>> g;
bool imp[maxn];
vector<pair<ll,pair<int,int>>> edges;
vector<int> a;
vector<vector<ll>> dists;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>k>>m;
g.resize(n);
for (int i=0; i<k; i++) {
int x;
cin>>x; --x;
imp[x]=true;
a.push_back(x);
}
dists.resize(k);
for (int i=0; i<m; i++) {
int u,v,w; cin>>u>>v>>w;
--u; --v;
edges.push_back({w,{u,v}});
g[u].push_back({w,v});
g[v].push_back({w,u});
}
for (int i=0; i<k; i++) {
spa(g,dists[i],a[i]);
}
ll res = inf;
// center of the tree (i)
for (int i=0; i<n; i++) {
ll cur = 0;
// dist from important node to i
for (int j=0; j<k; j++) {
cur += dists[j][i];
}
res = min(res, cur);
}
cout<<res<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |