Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Includes **/
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <chrono>
- #include <vector>
- #include <cstring>
- #include <set>
- #include <queue>
- using namespace std;
- /** Shortcuts **/
- #define fi first
- #define se second
- #define pb push_back
- #define pf push_front
- #define Pb pop_back
- #define Pf pop_front
- #define mp make_pair
- #define INF (int32)1e18
- typedef long int int32;
- typedef unsigned long int uint32;
- typedef long long int int64;
- typedef unsigned long long int uint64;
- typedef long long LL;
- typedef long double LD;
- /** Globals **/
- vector<LL> dijjjkstra(int s, int n, vector<vector<pair<int, LL>>> &edges) {
- vector<LL> d(n, INF);
- d[s] = 0;
- priority_queue<pair<LL, int>> q;
- q.push({0, s});
- while (!q.empty()) {
- int v = q.top().se;
- if (-q.top().fi > d[v]) {
- continue;
- }
- for (int j = 0; j < edges[v].size(); ++j) {
- int u = edges[v][j].fi;
- LL weight = edges[v][j].se;
- if (d[u] > d[v] + weight) {
- d[u] = d[v] + weight;
- q.push({-d[u], u});
- }
- }
- q.pop();
- }
- return d;
- }
- void solve() {
- int n, m;
- cin >> n >> m;
- vector<vector<pair<int, LL>>> edges(n);
- for (int i = 0; i < m; ++i) {
- int a, b;
- LL w;
- cin >> a >> b >> w;
- a--, b--;
- edges[a].emplace_back(b, w);
- }
- int a, b, c;
- cin >> a >> b >> c;
- a--, b--, c--;
- auto ansa = dijjjkstra(a, n, edges);
- auto ansb = dijjjkstra(b, n, edges);
- auto ansc = dijjjkstra(c, n, edges);
- LL ab = ansa[b];
- LL ac = ansa[c];
- LL bc = ansb[c];
- LL ba = ansb[a];
- LL cb = ansc[b];
- LL ca = ansc[a];
- LL minimum1 = min(ab + bc, min(bc + ca, ca + ab));
- LL minimum2 = min(ab + ac, min(ba+ bc, cb + ca));
- LL minimum3 = min(ca + ba, min(ac + cb, ba + ac));
- LL minimum4 = min(ba + ca, min(ab + cb, ac + bc));
- LL minimum = min(min(minimum1, minimum2),
- min(minimum3, minimum4));
- if (minimum >= INF) {
- cout << "-1\n";
- } else {
- cout << minimum << "\n";
- }
- }
- int main() {
- auto start = std::chrono::steady_clock::now();
- std::ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // freopen(".in", "r", stdin);
- // freopen(".out", "w", stdout);
- solve();
- auto finish = chrono::steady_clock::now();
- auto elapsed_ms = chrono::duration_cast<std::chrono::milliseconds>(finish - start);
- cerr << "Elapsed: " << elapsed_ms.count() << " ms\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement