This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;
int n, m, k;
bool marked[mxN];
vector<pair<int, int>> G[mxN];
pair<int, int> dijkstra_with_starting_point(int st) {
vector<int> dis(n + 1, 1e9);
priority_queue<pair<int, int>> pq;
pq.emplace(0, st); dis[st] = 0;
while (not pq.empty()) {
int v = pq.top().ss;
int cdis = -pq.top().ff;
pq.pop();
if (marked[v]) return {cdis, v};
if (cdis != dis[v]) continue;
for (auto [u, w] : G[v]) {
if (cdis + w < dis[u]) {
dis[u] = cdis + w;
pq.emplace(-dis[u], u);
}
}
}
return {1e9, -1};
}
pair<int, pair<int, int>> dijkstra_without_starting_point() {
vector<int> src(n + 1, 0);
vector<int> dis(n + 1, 1e9);
priority_queue<pair<int, int>> pq;
for (int i = 1; i <= n; ++i) if (marked[i]) {
pq.emplace(0, i);
dis[i] = 0, src[i] = i;
}
while (not pq.empty()) {
int v = pq.top().ss;
int cdis = -pq.top().ff;
pq.pop();
if (cdis != dis[v]) continue;
for (auto [u, w] : G[v]) {
if (cdis + w < dis[u]) {
src[u] = src[v];
dis[u] = cdis + w;
pq.emplace(-dis[u], u);
}
}
}
int res = 1e9, ru = -1, rv = -1;
for (int i = 1; i <= n; ++i) for (auto [u, w] : G[i]) {
if (src[i] != src[u] && dis[u] + dis[i] + w < res)
res = dis[u] + dis[i] + w, ru = src[i], rv = src[u];
}
return {res, {ru, rv}};
}
void solve() {
cin >> n >> m >> k;
for (int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
for (int i = 1; i <= k; ++i) {
int idx; cin >> idx;
marked[idx] = true;
}
int res = 1e9;
auto tmp = dijkstra_without_starting_point();
int s = tmp.ss.ff, t = tmp.ss.ss, dis1 = tmp.ff;
marked[s] = marked[t] = false;
tmp = dijkstra_without_starting_point();
res = min(res, dis1 + tmp.ff);
auto tmp1 = dijkstra_with_starting_point(s);
dis1 = tmp1.ff, marked[tmp1.ss] = false;
res = min(res, dis1 + dijkstra_with_starting_point(t).ff);
marked[tmp1.ss] = true;
auto tmp2 = dijkstra_with_starting_point(t);
dis1 = tmp2.ff, marked[tmp2.ss] = false;
res = min(res, dis1 + dijkstra_with_starting_point(s).ff);
marked[tmp2.ss] = false;
cout << res << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
cout << "Check array size pls sir" << endl;
#endif
}
# | 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... |