Submission #435034

#TimeUsernameProblemLanguageResultExecution timeMemory
435034arwaeystoamnegCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
666 ms26540 KiB
// EXPLOSION!
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include<chrono>

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define mp make_pair
#define rsz resize
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define cont continue
#define endl '\n'
//#define ednl '\n'
#define test int testc;cin>>testc;while(testc--)
#define pr(a, b) trav(x,a)cerr << x << b; cerr << endl;
#define message cout << "Hello World" << endl;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4000000000000000000LL;
const ll inf = 1000000007;//998244353    

void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) {
	F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; }
}void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; }
void setIO(string s) {
	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef arwaeystoamneg
	if (sz(s))
	{
		freopen((s + ".in").c_str(), "r", stdin);
		if (s != "test1")
			freopen((s + ".out").c_str(), "w", stdout);
	}
#endif
}
const int MAX = 111111;
int n, m, a, b, x, y;
struct edge
{
	int a, b, c;
	int get(int i)
	{
		return a + b - i;
	}
};
vi adj[MAX];
edge e[2 * MAX];
ll dista[MAX], distb[MAX], distx[MAX], disty[MAX];
ll dpx[2 * MAX], dpy[2 * MAX];
void dijkstra(int a, ll* d)
{
	fill(d, d + n, linf);
	d[a] = 0;
	priority_queue<pll, vpll, greater<pll>>todo;
	todo.push({ 0,a });
	while (sz(todo))
	{
		ll di = todo.top().f;
		int i = todo.top().s;
		todo.pop();
		if (d[i] != di)continue;
		trav(x, adj[i])
		{
			int j = e[x].get(i);
			if (d[j] > d[i] + e[x].c)
			{
				d[j] = d[i] + e[x].c;
				todo.push({ d[j],j });
			}
		}
	}
}
void Do(int st, ll* dp, ll* d)
{
	priority_queue<pll, vpll, greater<pll>>todo;
	F0R(i, m)
	{
		if (dista[e[i].b] < dista[e[i].a])swap(e[i].b, e[i].a);
		if (dista[e[i].a] + e[i].c + distb[e[i].b] != dista[b])
		{
			dp[i] = linf;
		}
		else
		{
			dp[i] = d[e[i].a];
			todo.push({ dp[i],i });
		}
	}
	while (sz(todo))
	{
		ll val = todo.top().f;
		int idx = todo.top().s;
		todo.pop();
		if (dp[idx] != val)continue;
		int i = e[idx].b;
		trav(x, adj[i])
		{
			int j = e[x].get(i);
			if (dista[i] + e[x].c + distb[j] != dista[b])continue;
			if (dp[x] > dp[idx])
			{
				dp[x] = dp[idx];
				todo.push({ dp[x],x });
			}
		}
	}
}
int main()
{
	setIO("test1");
	cin >> n >> m >> a >> b >> x >> y;
	a--, b--, x--, y--;
	F0R(i, m)
	{
		int x, y, z;
		cin >> x >> y >> z;
		adj[--x].pb(i);
		adj[--y].pb(i);
		e[i] = { x,y,z };
	}
	dijkstra(a, dista);
	dijkstra(b, distb);
	dijkstra(x, distx);
	dijkstra(y, disty);
	Do(x, dpx, distx);
	Do(y, dpy, disty);
	ll ans = distx[y];
	F0R(i, m)
	{
		ans = min(ans, dpx[i] + disty[e[i].b]);
		ans = min(ans, dpy[i] + distx[e[i].b]);
	}
	cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...