Submission #424426

#TimeUsernameProblemLanguageResultExecution timeMemory
424426nkatoCommuter Pass (JOI18_commuter_pass)C++17
15 / 100
2086 ms24568 KiB
#include <bits/stdc++.h>
using namespace std;

/////////////////////// MACROS ////////////////////////////////////////////
using ll = long long;
using ld = long double;
using db = double;
using str = string;

using pi = pair<int,int>;
using pl = pair<ll,ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<str>;
using vpi = vector<pi>;

#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcTU> using P = pair<T,U>;
tcT> using PR = pair<T,T>;
tcTU> using omap = unordered_map<T, U>;
tcT> using oset = unordered_set<T>;

#define mp make_pair
#define fi first
#define se second

#define sz(x) int((x).size())
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound

// LOOPS
#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)

/////////////////////// IMPORANT VARS /////////////////////////////////////

const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INFL = 1e18+10;
const int INF = 1e9+10;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
tcT> using pql = priority_queue<T,vector<T>,less<T>>;

tcT> bool ckmin(T& a, const T& b) {
	return b < a ? a = b, 1 : 0; } 
tcT> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

/////////////////////// TO_STRING /////////////////////////////////////////
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
	#ifdef LOCAL
		return b ? "true" : "false";
	#else
		return ts((int)b);
	#endif
}

tcT> str ts(T v) {
	#ifdef LOCAL
		bool fst = 1; str res = "{";
		for (const auto& x: v) {
			if (!fst) res += ", ";
			fst = 0; res += ts(x);
		}
		res += "}"; return res;
	#else
		bool fst = 1; str res = "";
		for (const auto& x: v) {
			if (!fst) res += " ";
			fst = 0; res += ts(x);
		}
		return res;

	#endif
}
tcTU> str ts(pair<T,U> p) {
	#ifdef LOCAL
		return "("+ts(p.fi)+", "+ts(p.se)+")";
	#else
		return ts(p.fi)+" "+ts(p.se);
	#endif
}

///////////////////////// DEBUG ///////////////////////////////////////////
#define tcTUU tcT, class ...U
void DBG() { cerr << "]" << endl; }
tcTUU> void DBG(const T& t, const U&... u) {
	cerr << ts(t); if (sizeof...(u)) cerr << ", ";
	DBG(u...); }
#ifdef LOCAL
	#define _GLIBCXX_DEBUG
	#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
	#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
		 << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
	#define dbg(...) 0
	#define chk(...) 0
#endif

///////////////////////// FILE I/O ////////////////////////////////////////
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setPrec() { cout << fixed << setprecision(15); }
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
	unsyncIO(); setPrec();
	#ifndef LOCAL	
		if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
	#endif
}

///////////////////////// TEMPLATE ABOVE //////////////////////////////////
// TODO
// - Don't focus on only one approach
// - Read Problem Fully
// - Think of Edges Cases
// - Implement carefully
const int nax = 1e5+10;
ll dp[nax][2], dst[nax][5];
vpi adj[nax];
vi chd[nax];
bool vis[nax];
int n, m, a, b, c, d;
ll ans = 0;

void reset() {
    F0R(i, nax) {
        fill(dst[i], dst[i]+5, INFL);
        fill(dp[i], dp[i]+2, INFL);
    }
}

void dijkstra1(int x, int t) {
    fill(vis, vis+nax, 0);
    dst[x][t] = 0;
    pqg<pi> q;
    q.push(mp(0, x));
    while(!q.empty()) {
        int u = q.top().se;
        q.pop();

        if (vis[u]) continue;
        vis[u] = 1;

        int v, w;
        trav(e, adj[u]) {
            tie(v, w) = e;
            if (ckmin(dst[v][t], dst[u][t]+w)) {
                chd[v] = {u};
                q.push(mp(dst[v][t], v));
            } else if (dst[v][t] == (dst[u][t]+w)) {
                chd[v].pb(u);
            } 
        }
    }
}

void dijkstra2(int x, int t) {
    fill(vis, vis+nax, 0);
    dst[x][t] = 0;
    pqg<pi> q;
    q.push(mp(0, x));
    while(!q.empty()) {
        int u = q.top().se;
        q.pop();


        if (vis[u]) continue;
        vis[u] = 1;

        int v, w; 
        trav(e, adj[u]) {
            tie(v, w) = e;
            if (ckmin(dst[v][t], dst[u][t]+w)) {
                q.push(mp(dst[v][t], v));
            }
        }
    } 
}

void dfs(int u) {
    ckmin(dp[u][0], dst[u][0]);
    ckmin(dp[u][1], dst[u][1]);
    trav(v, chd[u]) {
        ckmin(dp[v][0], dp[u][0]);
        ckmin(dp[v][1], dp[u][1]);
        dfs(v);
    }
    dbg(u, dp[u][0], dp[u][1]);
    ckmin(ans, dp[u][0]+dp[u][1]);
    fill(dp[u], dp[u]+2, INFL);
}

int main() {
	setIO(); 
    reset();



    cin >> n >> m >> a >> b >> c >> d;

    F0R(i, m) {
        int u, v, cst; cin >> u >> v >> cst;
        adj[u].pb(mp(v, cst));
        adj[v].pb(mp(u, cst));
    }
   
    dijkstra2(c, 0);
    dijkstra2(d, 1);
    ans = dst[d][0];

    fill(chd, chd+nax, vi{});
    dijkstra1(a, 2);
    FOR(i, 1, n+1) dbg(i, chd[i]);
    dfs(b); 

    fill(chd, chd+nax, vi{});
    dijkstra1(b, 3);
    FOR(i, 1, n+1) dbg(i, chd[i]);
    dfs(a);

    cout << ans << endl;

	exit(0);
}

Compilation message (stderr)

commuter_pass.cpp: In function 'void dfs(int)':
commuter_pass.cpp:118:19: warning: statement has no effect [-Wunused-value]
  118 |  #define dbg(...) 0
      |                   ^
commuter_pass.cpp:211:5: note: in expansion of macro 'dbg'
  211 |     dbg(u, dp[u][0], dp[u][1]);
      |     ^~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:118:19: warning: statement has no effect [-Wunused-value]
  118 |  #define dbg(...) 0
      |                   ^
commuter_pass.cpp:236:20: note: in expansion of macro 'dbg'
  236 |     FOR(i, 1, n+1) dbg(i, chd[i]);
      |                    ^~~
commuter_pass.cpp:118:19: warning: statement has no effect [-Wunused-value]
  118 |  #define dbg(...) 0
      |                   ^
commuter_pass.cpp:241:20: note: in expansion of macro 'dbg'
  241 |     FOR(i, 1, n+1) dbg(i, chd[i]);
      |                    ^~~
commuter_pass.cpp: In function 'void setIn(str)':
commuter_pass.cpp:125:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp: In function 'void setOut(str)':
commuter_pass.cpp:126:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 | void setOut(str s) { freopen(s.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...