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 int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
struct edge {
int from, to, wei;
bool operator < (const edge &other) const {
return wei > other.wei;
}
};
const int maxn = 1e5;
const int inf = 1e16 + 42;
int n, k, m;
vector<edge> graph[1 + maxn];
bool special[1 + maxn];
vector<int> specials;
void dijkstra(int source, vector<int> &dist) {
dist.assign(1 + n, -1);
// dist[source] = 0;
priority_queue<edge> q;
q.push({0, source, 0});
while(!q.empty()) {
edge cur = q.top();
q.pop();
if(dist[cur.to] != -1) {
continue;
}
dist[cur.to] = cur.wei;
for(const edge &nei : graph[cur.to]) {
if(dist[nei.to] == -1 || dist[nei.to] /*<*/> dist[cur.to] + nei.wei) {
q.push({cur.to, nei.to, dist[cur.to] + nei.wei});
}
}
}
}
void solve() {
cin >> n >> k >> m;
for(int i = 0; i < k; i++) {
int x;
cin >> x;
special[x] = true;
specials.pb(x);
}
for(int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
graph[a].pb({a, b, c});
graph[b].pb({b, a, c});
}
int ans = inf;
vector<vector<int> > distances(k);
for(int i = 0; i < k; i++) {
dijkstra(specials[i], distances[i]);
}
for(int i = 1; i <= n; i++) {
int cur = 0;
for(int j = 0; j < k; j++) {
cur += distances[j][i];
}
ans = min(ans, cur);
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
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... |