이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |