Submission #990475

#TimeUsernameProblemLanguageResultExecution timeMemory
990475SN0WM4NCommuter Pass (JOI18_commuter_pass)C++14
0 / 100
885 ms81916 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sort undefined_function // To use stable_sort instead sort 
#define bpc __builtin_popcount
#define ull unsigned long long
#define ld double
#define ll long long
#define mp make_pair
#define F first
#define S second
 
#pragma GCC optimize("O3")
 
#ifdef LOCAL 
	#include "debug.h"
#else 
	#define dbg(...) 0
#endif
 
using namespace __gnu_pbds;
using namespace std;
 
typedef tree<long long, null_type, less_equal<long long>,
    rb_tree_tag, tree_order_statistics_node_update> Tree;
 
const ll INF = 9223372036854775807LL;
const ll inf = 2147483647;
const ll MOD = 1e9 + 7; //[998244353, 1e9 + 7, 1e9 + 13]
const ld PI = acos(-1);
const ll NROOT = 500;
 
ll binpow(ll a, ll b, ll _MOD = -1) {
	if (_MOD == -1)
		_MOD = MOD;
	ll res = 1;
	for (; b; b /= 2, a *= a, a %= _MOD)
		if (b & 1) res *= a, res %= _MOD;
	return res;
}
 
void set_IO(string s) {
#ifndef LOCAL
	string in  = s +  ".in";
	string out = s + ".out";
	freopen(in.c_str(),  "r",  stdin);
	freopen(out.c_str(), "w", stdout);
#endif
}
 
bool dataOverflow(ll a, ll b) {return (log10(a) + log10(b) >= 18);}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}
ll ceil(ll a, ll b) {return (a + b - 1) / b;}
ll invmod(ll a) {return binpow(a, MOD - 2);}
 
#define int long long
 
int n, m;
vector<vector<pair<int, ll>>> g;
 
int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
 
	cin >> n >> m;
	g.resize(n + 1);
	int s, t, u, v;
	cin >> s >> t >> u >> v;
	vector<pair<pair<int, int>, ll>> edges; 
	map<pair<int, int>, int> ID, old;
 
	for (int i = 1; i <= m; i ++) {
		int a, b; ll c;
		cin >> a >> b >> c;
 
		ID[{b, a}] = ID[{a, b}] = edges.size();
		old[{b, a}] = old[{a, b}] = c;
		edges.push_back({{a, b}, c});
	
		g[a].push_back({b, c});
		g[b].push_back({a, c});
	}
 
	using T = pair<ll, int>;
	priority_queue<T, vector<T>, greater<T>> q;
	q.push({0, s});
 
	vector<ll> dis(n + 1, INF);
 
	while (!q.empty()) {
		auto [d, x] = q.top();
		q.pop();
 
		if (dis[x] <= d)
			continue;
		dis[x] = d;
 
		for (auto &y : g[x])
			q.push({d + y.S, y.F});
	}
 
	vector<bool> vis(n + 1);
 
	q.push({dis[t], t});
	while (!q.empty()) {
		auto [d, x] = q.top();
		q.pop();
 
		if (vis[x])
			continue;
		vis[x] = 1;
 
		for (auto &y : g[x]) {
			if (dis[y.F] + y.S == dis[x]) {
				q.push({dis[y.F], y.F});
				edges[ID[{x, y.F}]].S = 0;
			}
		}
	}
 
	vector<vector<int>> g1(n + 1), g2(n + 1);
	vector<int> in1(n + 1), in2(n + 1);
 
	for (auto &x : edges) {
		auto [a, b] = x.F;
		ll c = x.S;
 
		if (c == 0) {
			dbg(a, b);
			if (dis[a] < dis[b]) {
				g1[a].push_back(b);
				g2[b].push_back(a);
				in1[b] ++; in2[a] ++;
			} else {	
				g1[b].push_back(a);
				g2[a].push_back(b);
				in1[a] ++; in2[b] ++;
			}
		}	
	}	
 
	vector<ll> disU(n + 1, INF), disV(n + 1, INF);
	vector<ll> ans1(n + 1, INF), ans2(n + 1, INF);
 
	q.push({0, u});
	while (!q.empty()) {
		auto [d, x] = q.top();
		q.pop();
 
		if (disU[x] <= d)
			continue;
		disU[x] = d;
 
		for (auto &y : g[x]) 
			q.push({d + y.S, y.F});
	}
 
	q.push({0, v});
	while (!q.empty()) {
		auto [d, x] = q.top();
		q.pop();
 
		if (disV[x] <= d)
			continue;
		disV[x] = d;
 
		for (auto &y : g[x]) 
			q.push({d + y.S, y.F});
	}
 
	queue<T> que;
	que.push({s, disV[s]});
	que.push({t, disV[t]});
	while (!que.empty()) {
		auto [x, d] = que.front();
		que.pop();
		d = min(d, disV[x]);
		ans1[x] = min(ans1[x], d);
		for (auto &y : g1[x]) {
			in1[y] --;
			ans1[y] = min(ans1[y], d);
			if (in1[y] == 0)
				que.push({y, d});
		}
	}
	
	que.push({s, disV[s]});
	que.push({t, disV[t]});
	while (!que.empty()) {
		auto [x, d] = que.front();
		que.pop();
		d = min(d, disV[x]);
		ans2[x] = min(ans2[x], d);
		for (auto &y : g2[x]) {
			in2[y] --;
			ans2[y] = min(ans2[y], d);
			if (in2[y] == 0)
				que.push({y, d});
		}
	}
 
	dbg(ans1, ans2);
 
	int ans = disU[v];
	for (int i = 1; i <= n; i ++) {
		if (ans1[i] != INF) {
			ans = min(ans, ans1[i] + disU[i]);
			dbg(ans, ans1[i], disU[i]);
		}
		if (ans2[i] != INF) {
			ans = min(ans, disU[i] + ans2[i]);
			dbg(ans, ans2[i], disU[i]);
		}
	}
 
	cout << ans << "\n";
 
 
	cout << ans << "\n";
	return 0;
}

Compilation message (stderr)

commuter_pass.cpp: In function 'int32_t main()':
commuter_pass.cpp:92:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   92 |   auto [d, x] = q.top();
      |        ^
commuter_pass.cpp:107:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  107 |   auto [d, x] = q.top();
      |        ^
commuter_pass.cpp:126:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  126 |   auto [a, b] = x.F;
      |        ^
commuter_pass.cpp:18:19: warning: statement has no effect [-Wunused-value]
   18 |  #define dbg(...) 0
      |                   ^
commuter_pass.cpp:130:4: note: in expansion of macro 'dbg'
  130 |    dbg(a, b);
      |    ^~~
commuter_pass.cpp:148:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  148 |   auto [d, x] = q.top();
      |        ^
commuter_pass.cpp:161:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  161 |   auto [d, x] = q.top();
      |        ^
commuter_pass.cpp:176:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  176 |   auto [x, d] = que.front();
      |        ^
commuter_pass.cpp:191:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  191 |   auto [x, d] = que.front();
      |        ^
commuter_pass.cpp:18:19: warning: statement has no effect [-Wunused-value]
   18 |  #define dbg(...) 0
      |                   ^
commuter_pass.cpp:203:2: note: in expansion of macro 'dbg'
  203 |  dbg(ans1, ans2);
      |  ^~~
commuter_pass.cpp:18:19: warning: statement has no effect [-Wunused-value]
   18 |  #define dbg(...) 0
      |                   ^
commuter_pass.cpp:209:4: note: in expansion of macro 'dbg'
  209 |    dbg(ans, ans1[i], disU[i]);
      |    ^~~
commuter_pass.cpp:18:19: warning: statement has no effect [-Wunused-value]
   18 |  #define dbg(...) 0
      |                   ^
commuter_pass.cpp:213:4: note: in expansion of macro 'dbg'
  213 |    dbg(ans, ans2[i], disU[i]);
      |    ^~~
commuter_pass.cpp: In function 'void set_IO(std::string)':
commuter_pass.cpp:46:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |  freopen(in.c_str(),  "r",  stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:47:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |  freopen(out.c_str(), "w", stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...