Submission #152370

#TimeUsernameProblemLanguageResultExecution timeMemory
15237012tqianCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
826 ms45076 KiB
#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const double PI = 4 * atan(1);

#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>

#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)

template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
  cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}

void fast_io(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
void io(string taskname){
    string fin = taskname + ".in";
    string fout = taskname + ".out";
    const char* FIN = fin.c_str();
    const char* FOUT = fout.c_str();
    freopen(FIN, "r", stdin);
   // freopen(FOUT, "w", stdout);
    fast_io();
}
const int MAX = 1e5 + 5;
const ll INF = 1e18;
int n, m, s, t, u, v;
vector<pair<int, ll>> adj[MAX];
vi dag[MAX];
vi undo[MAX];
int par[MAX];
vector<ll> use[MAX];
int vis[MAX];
vector<pair<pii, ll>> edges;

int check(int x){
	if(use[s][x] + use[t][x] == use[s][t]) return 1;
	return 0;
}
void dfs(int src){
	vis[src] = 1;
	for(auto nxt: adj[src]){
		int neigh = nxt.f;
		int weight = nxt.s;
		if(vis[neigh] == 0 && check(neigh) && weight + use[s][src] == use[s][neigh]){
			dag[src].eb(neigh);
			dfs(neigh);
		}
	}
}
vector<ll> dijkstra(int st) {
    vector<ll> dist;
	f0r(i, n){
		dist.eb(INF);
	}
    dist[st] = 0;
    // <distance, <previous vertex, current vertex>>
    priority_queue<pair<ll, pair<int, int>>, vector<pair<ll, pair<int, int>> >, greater<pair<ll, pair<int ,int>> > > pq;
    pq.emplace(0, make_pair(st, st));

    while(!pq.empty()){
        auto top = pq.top();
        pq.pop();

        ll d = top.first;
        int prev = top.second.first;
        int cur = top.second.second;

        if(d > dist[cur])
            continue;

        par[cur] = prev;

        for(auto v: adj[cur]){
            int nxt = v.first;
            ll w = v.second;
            if (dist[nxt] > dist[cur] + w) {
                dist[nxt] = dist[cur] + w;
                pq.emplace(dist[nxt], make_pair(cur, nxt));
            }
        }
    }
    return dist;
}
template<int SZ> struct TopoSort {
    int N, in[SZ];
    vi res, adj[SZ];

    void addEdge(int x, int y) { adj[x+1].pb(y+1), in[y+1] ++; }

    bool sort(int _N) {
        N = _N; queue<int> todo;
        f1r(i,1,N+1) if (in[i] == 0) todo.push(i);
        while (sz(todo)) {
            int x = todo.front(); todo.pop(); res.pb(x);
            trav(i,adj[x]) {
                in[i] --;
                if (!in[i]) todo.push(i);
            }
        }
        return sz(res) == N;
    }
};
TopoSort<MAX> top;
ll dp1[MAX];
ll dp2[MAX];
vi order;
ll ans = INF;
void solve(){
    int sz = sz(order);
    for(int i: order){
        dp1[i] = use[v][i];
        for(int nxt: undo[i]){
            dp1[i] = min(dp1[i], dp1[nxt]);
        }

    }
    for(int it = sz-1; it>= 0; it--){
        int i = order[it];
        dp2[i] = use[v][i];
        for(int nxt: dag[i]){
            dp2[i] = min(dp2[i], dp2[nxt]);
        }
    }
    for(int i: order){
      //  cout << i << " " << dp1[i] << " " << dp2[i] << " " << use[u][i] << endl;
        ans = min(ans, min(dp1[i], dp2[i]) + use[u][i]);
    }
}

map<pii, int> um;
int main(){
   // io("test");
    fast_io;
    cin >> n >> m >> s >> t >> u >> v;
    s--; t--; u--; v--;
    f0r(i, m){
    	ll ai, bi, ci;
    	cin >> ai >> bi >> ci;
    	ai--; bi--;
    	adj[ai].eb(mp(bi, ci));
    	adj[bi].eb(mp(ai, ci));
    	edges.eb(mp(mp(ai, bi), ci));
    }
    use[s] = dijkstra(s);
    use[t] = dijkstra(t);
    use[u] = dijkstra(u);
    use[v] = dijkstra(v);
    for(auto e: edges){
        int from = e.f.f;
        int to = e.f.s;
        ll weight = e.s;
        if(check(to) && check(from)){
            if(use[s][from] + use[t][to] + weight == use[s][t]){
                dag[from].eb(to);
                undo[to].eb(from);
                top.addEdge(from, to);
            }
            if(use[s][to] + use[t][from] + weight == use[s][t]){
                dag[to].eb(from);
                undo[from].eb(to);
                top.addEdge(to, from);
            }

        }
    }
    top.sort(n);
  //  cout << top.res << endl;
    for(int x: top.res) if(check(x-1)) order.eb(x-1);
    solve();
   // cout << order << " " << s << " " << t << endl;
   // dag_dfs(s, mp(use[u][s], use[v][s]));
    ans = min(ans, use[u][v]);
    cout << ans << endl;
    return 0;
}

Compilation message (stderr)

commuter_pass.cpp:1:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/stack:200000000")
 
commuter_pass.cpp: In function 'void io(std::__cxx11::string)':
commuter_pass.cpp:53:17: warning: unused variable 'FOUT' [-Wunused-variable]
     const char* FOUT = fout.c_str();
                 ^~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:167:12: warning: statement is a reference, not call, to function 'fast_io' [-Waddress]
     fast_io;
            ^
commuter_pass.cpp:167:12: warning: statement has no effect [-Wunused-value]
commuter_pass.cpp: In function 'void io(std::__cxx11::string)':
commuter_pass.cpp:54:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(FIN, "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...