제출 #548141

#제출 시각아이디문제언어결과실행 시간메모리
548141AA_SurelyCommuter Pass (JOI18_commuter_pass)C++17
0 / 100
2078 ms76532 KiB
#include <bits/stdc++.h>

#define FOR(i,x,n) 	for(int i=x; i<n; i++)
#define F0R(i,n) 	FOR(i,0,n)
#define ROF(i,x,n) 	for(int i=n-1; i>=x; i--)
#define R0F(i,n) 	ROF(i,0,n)

#define WTF 		cout << "WTF" << endl

#define IOS 		ios::sync_with_stdio(false); cin.tie(0)
#define F 			first
#define S	 		second
#define pb 			push_back

#define ALL(x) 		x.begin(), x.end()
#define RALL(x) 	x.rbegin(), x.rend()

using namespace std;
typedef long long 		LL;

typedef pair<int, int> 	PII;
typedef pair<LL, LL> 	PLL;

typedef vector<int> 	VI;
typedef vector<LL> 		VLL;
typedef vector<PII> 	VPII;
typedef vector<PLL> 	VPLL;

const int N = 2e5 + 7;
const int ALPHA = 27;
const LL INF = 1e18;
const int MOD = 1e9 + 7;
const int LOG = 22;

int n, m, s, t, u, v;
int deg[N], rm[N];
bool colored[N];

LL dist[N], sdist[N], udist[N], vdist[N];
LL mnu[N], mnv[N];

VPLL adj[N];
VI dag[N], par[N], order, rdag[N];

void sdijk() {
    fill(sdist, sdist + n, INF);
    
    sdist[s] = 0;
    priority_queue<PLL> keep;
    keep.push({0, s});

    while(!keep.empty()) {
        int now = keep.top().S;
        if (dist[now] != -keep.top().F) {
            keep.pop();
            continue;
        }

        keep.pop();

        for(auto [on, w] : adj[now]) {
            if (sdist[on] > sdist[now] + w) {
                sdist[on] = sdist[now] + w;
                par[on].clear();
                par[on].pb(now);

                keep.push({-dist[on], on});
            }

            if (sdist[on] == sdist[now] + w) 
                par[on].pb(now);
        }
    }

    F0R(i, n) {
        sort(ALL(par[i]));
        par[i].resize(unique(ALL(par[i])) - par[i].begin());
    }

    return;
}

void odijk(int sc) {
    fill(dist, dist + n, INF);
    dist[sc] = 0;

    priority_queue<PLL> keep;
    keep.push({0, sc});

    while(!keep.empty()) {
        int now = keep.top().S;
        if (dist[now] != -(keep.top().F)) {
            keep.pop();
            continue;
        }
        keep.pop();

        for(auto [on, w] : adj[now]) if (dist[on] > dist[now] + w) {
            dist[on] = dist[now] + w;
            keep.push({-dist[on], on});
        }
    }

    return;
}

void clarify() {
    F0R(i, n) for(int on : par[i])
        deg[on]++;
    
    queue<int> dl;
    F0R(i, n) if (!deg[i] && i != t) 
        dl.push(i);
        
    while(!dl.empty()) {
        int now = dl.front();
        dl.pop();

        for(int on : par[now]) {
            deg[on]--;
            if (!deg[on]) dl.push(on);
        }

        rm[now] = 1;
    }

    F0R(i, n) if (!rm[i]) for(int on : par[i]) 
        if (!rm[on]) {
            dag[on].pb(i);
            rdag[i].pb(on);
        }

    return;
}

void dfs(int now) {
    colored[now] = 1;
    for(int on : dag[now]) if (!colored[on]) 
        dfs(on);

    order.pb(now);
    return;
}

int main() {
    IOS;
    
    cin >> n >> m >> s >> t >> u >> v;
    s--; t--; v--; u--;

    F0R(i, m) {
        LL a, b, w; cin >> a >> b >> w;
        adj[--a].pb({--b, w});
        adj[b].pb({a, w});
    }

    /*F0R(i, n) {
        cout << i << " : ";
        for(auto [on, w] : adj[i]) cout << on << ' ';
        cout << endl;
    }*/

    sdijk();
    assert(0);

    odijk(u);
    F0R(i, n) udist[i] = dist[i];
    odijk(v);
    F0R(i, n) vdist[i] = dist[i];

    clarify();

    dfs(s);
    reverse(ALL(order));

    fill(mnu, mnu + n, INF);
    fill(mnv, mnv + n, INF);

    LL ans = INF;
    
    for(int now : order) {

        for(int on : rdag[now]) {
            mnu[now] = min(mnu[now], mnu[on]);
            mnv[now] = min(mnv[now], mnv[on]);
        }

        mnu[now] = min(mnu[now], udist[now]);
        mnv[now] = min(mnv[now], vdist[now]);

        ans = min({ans, udist[now] + mnv[now], vdist[now] + mnu[now]});
    }

    ans = min(ans, udist[v]);

    /*F0R(i, n) {
        cout << i << " : ";
        for(int on : par[i]) cout << on << ' ';
        cout << endl;
    }*/

    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...