Submission #207755

#TimeUsernameProblemLanguageResultExecution timeMemory
207755mode149256Commuter Pass (JOI18_commuter_pass)C++14
100 / 100
566 ms19572 KiB
/*input
10 15
6 8
7 9
2 7 12
8 10 17
1 3 1
3 8 14
5 7 15
2 3 7
1 10 14
3 6 12
1 5 10
8 9 1
2 9 7
1 4 1
1 8 1
2 4 7
5 6 16
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;

#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"

const int MOD = 1000000007;
const ll INF = (ll)1e15;
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;

template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }

template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}

int N, M, S, T, U, V;
vpi edges[MX];
ll minDist;

void find(int start, vl &dist) {
	dist = vl(N, INF);
	dist[start] = 0;

	priority_queue<pl, vpl, greater<pl>> pq;

	pq.push({0, start});

	while (pq.size()) {
		pl curr = pq.top(); pq.pop();

		int c = (int)curr.y;
		ll d = curr.x;

		if (d > dist[c]) continue;

		for (auto u : edges[c]) {
			if (dist[u.x] > d + u.y) {
				dist[u.x] = d + u.y;
				pq.push({dist[u.x], u.x});
			}
		}
	}
}

// dp[i] = min(fromU[j]) f.a. j on shortest path T-S to i
void doDP(int st, vl &dp, const vl &fromU, const vl &fromPir, const vl &fromKitas) {
	dp = vl(N, INF);
	dp[st] = fromU[st];

	priority_queue<pl, vpl, greater<pl>> pq;

	pq.push({dp[st], st});

	while (pq.size()) {
		pl curr = pq.top(); pq.pop();

		int c = (int)curr.y;
		ll d = curr.x;

		if (d > dp[c]) continue;

		for (auto u : edges[c]) {
			// printf("d = %lld, u = %d %d, dist = %lld\n", d, u.x, u.y, d + u.y + fromKitas[u.x]);
			if (fromPir[c] + u.y + fromKitas[u.x] == minDist and min(dp[c], fromU[u.x]) < dp[u.x]) {
				dp[u.x] = min(dp[c], fromU[u.x]);
				pq.push({dp[u.x], u.x});
			}
		}
	}
}


int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N >> M >> S >> T >> U >> V;
	S--; T--; U--; V--;
	for (int i = 0; i < M; ++i)
	{
		int a, b, w;
		cin >> a >> b >> w; a--; b--;
		edges[a].emplace_back(b, w);
		edges[b].emplace_back(a, w);
	}

	vl fromU, fromV;
	vl fromT, fromS;
	find(U, fromU);
	find(V, fromV);
	find(T, fromT);
	find(S, fromS);

	minDist = fromS[T];

	// void doDP(int st, vl & dp, const vl & fromU, const vl & fromS) {

	vl dpT, dpS;
	doDP(T, dpT, fromU, fromT, fromS);
	doDP(S, dpS, fromU, fromS, fromT);

	// print(dpT);
	// print(dpS);
	vl dist(N, INF);
	dist[T] = 0;

	priority_queue<pl, vpl, greater<pl>> pq;

	pq.push({0, T});

	ll ats = fromU[V];

	while (pq.size()) {
		pl curr = pq.top(); pq.pop();

		int c = (int)curr.y;
		ll d = curr.x;

		// printf("c = %d, fromV = %lld, dpt = %lld, dps = %lld, ats = %lld\n", c,
		// fromV[c], dpT[c], dpS[c], ats);
		ats = min(ats, fromV[c] + min(dpT[c], dpS[c]));

		if (d > dist[c]) continue;

		for (auto u : edges[c]) {
			if (d + u.y + fromS[u.x] == minDist and dist[u.x] == INF) {
				dist[u.x] = d + u.y;
				pq.push({dist[u.x], u.x});
			}
		}
	}

	printf("%lld\n", ats);
}

/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...