Submission #379864

#TimeUsernameProblemLanguageResultExecution timeMemory
379864SavicSCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
469 ms26088 KiB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll,ll> pii;
const int maxn = 100005;
const ll inf = 1e18 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n, m, s, t, a, b;

vector<pii> g[maxn];

ll dists[maxn];
ll distt[maxn];
ll dista[maxn];
ll distb[maxn];
void dij(int sv, ll dist[maxn]){
	ff(i,1,n)dist[i] = inf;
	priority_queue<pii,vector<pii>, greater<pii>> pq;
	pq.push({dist[sv] = 0, sv});
	while(!pq.empty()){
		pii v = pq.top(); pq.pop();
		if(dist[v.se] < v.fi)continue;
		for(auto c : g[v.se]){
			int u = c.fi;
			ll w = c.se;
			if(dist[u] > w + v.fi){
				dist[u] = w + v.fi;
				pq.push({dist[u], u});
			}
		}
	} 
}

ll rez = 0;

ll dp[maxn];
bool visited[maxn];
void dfs(int v, bool dir){
	visited[v] = 1;
	dp[v] = dista[v];
	for(auto c : g[v]){
		int u = c.fi;
		ll w = c.se;
		if(dir == 0){
			if(dists[v] + w + distt[u] == dists[t]){	
				if(!visited[u])dfs(u, dir);
				dp[v] = min(dp[v], dp[u]);
			}	
		}
		else{
			if(distt[v] + w + dists[u] == distt[s]){
				if(!visited[u])dfs(u, dir);
				dp[v] = min(dp[v], dp[u]);
			}	
		}
	}
	rez = min(rez, dp[v] + distb[v]);
}

int main()
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
	cin >> n >> m >> s >> t >> a >> b;
	ff(i,1,m){
		int u, v;
		ll w;
		cin >> u >> v >> w;
		g[u].pb({v, w});
		g[v].pb({u, w});
	}
	dij(s, dists);
	dij(t, distt);
	dij(a, dista);
	dij(b, distb);
	rez = dista[b];
	dfs(s, 0);
	ff(i,1,n)visited[i] = 0;
	dfs(t, 1);
	cout << rez << endl;
	return 0;
}
/**

6 6
1 6
1 4
1 2 1
2 3 1
3 5 1
2 4 3
4 5 2
5 6 1

// probati bojenje sahovski ili slicno

**/

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...