Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define cin_2d(vec, n, m) for(int i = 0; i < n; i++) for(int j = 0; j < m && cin >> vec[i][j]; j++);
- #define cout_2d(vec, n, m) for(int i = 0; i < n; i++, cout << "\n") for(int j = 0; j < m && cout << vec[i][j] << " "; j++);
- #define cout_map(mp) for(auto& [f, s] : mp) cout << f << " " << s << "\n";
- #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
- #define fixed(n) fixed << setprecision(n)
- #define ceil(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
- #define fill(vec, value) memset(vec, value, sizeof(vec));
- #define Num_of_Digits(n) ((int)log10(n) + 1)
- #define mod_combine(a, b, m) (((a % m) * (b % m)) % m)
- #define all(vec) vec.begin(), vec.end()
- #define rall(vec) vec.rbegin(), vec.rend()
- #define sz(x) int(x.size())
- #define debug(x) cout << #x << ": " << (x) << "\n";
- #define fi first
- #define se second
- #define ll long long
- #define ull unsigned long long
- #define Mod 1'000'000'007
- #define OO 2'000'000'000
- #define EPS 1e-9
- #define PI acos(-1)
- template < typename T = int > using Pair = pair < T, T >;
- template < typename T = int > istream& operator >> (istream &in, vector < T > &v) {
- for (auto &x: v) in >> x;
- return in;
- }
- template < typename T = int > ostream& operator << (ostream &out, const vector < T > &v) {
- for (const T &x: v) out << x << ' ';
- return out;
- }
- void AhMeD_HoSSaM(){
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- // #ifndef ONLINE_JUDGE
- // freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- // #endif
- }
- struct Edge {
- ll v, w;
- Edge(int V = 0, int W = 0): v(V), w(W) {}
- bool operator < (const Edge& e) const {
- return w > e.w;
- }
- };
- vector < vector < Edge > > adj, rev_adj;
- int n, m;
- vector < ll > Dijkstra(ll src, vector < vector < Edge > >& G){
- vector < ll > dist(sz(G), LLONG_MAX);
- dist[src] = 0;
- priority_queue < Edge > pq;
- pq.push(Edge(src, 0));
- while(!pq.empty()){
- auto [u, cost] = pq.top();
- pq.pop();
- if(dist[u] < cost) continue;
- for(auto& [v, w] : G[u]){
- if(dist[v] > dist[u] + w){
- dist[v] = dist[u] + w;
- pq.push(Edge(v, dist[v]));
- }
- }
- }
- return dist;
- }
- ll try_node(ll dest, ll src1, ll src2){
- vector < ll > dist1 = Dijkstra(src1, adj);
- vector < ll > dist2 = Dijkstra(src2, adj);
- vector < ll > dist_d = Dijkstra(dest, rev_adj);
- ll ans = LLONG_MAX;
- for(int i = 1; i <= n; i++){
- if(dist1[i] == LLONG_MAX || dist2[i] == LLONG_MAX || dist_d[i] == LLONG_MAX) continue;
- ans = min(ans, dist1[i] + dist2[i] + dist_d[i]);
- }
- return (ans == LLONG_MAX ? LLONG_MAX : ans);
- }
- void Solve(){
- cin >> n >> m;
- adj = vector < vector < Edge > > (n + 10);
- rev_adj = vector < vector < Edge > > (n + 10);
- for(int i = 0, u, v, w; i < m && cin >> u >> v >> w; i++)
- adj[u].push_back(Edge(v, w)), rev_adj[v].push_back(Edge(u, w));
- ll x, y, z;
- cin >> x >> y >> z;
- vector < Pair < ll > > ans = {{try_node(x, y, z), x}, {try_node(y, x, z), y}, {try_node(z, x, y), z}};
- sort(all(ans), [&](const Pair < ll > &a, const Pair < ll > &b){
- return a.fi < b.fi || (a.fi == b.fi && a.se < b.se);
- });
- if(ans[0].fi >= OO) return cout << -1 << '\n', void();
- cout << ans[0].se << ' ' << ans[0].fi << '\n';
- }
- int main(){
- AhMeD_HoSSaM();
- int t = 1;
- //cin >> t;
- while(t--)
- Solve();
- return 0;
- }
Add Comment
Please, Sign In to add comment