Submission #1215534

#TimeUsernameProblemLanguageResultExecution timeMemory
1215534mactoddloverCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
330 ms32660 KiB
#include<bits/stdc++.h>

#define endl "\n"

using namespace std;

typedef long long ll;

const int N = 2e5 + 15;
const ll INF = 1e18;

int n, m;
int U, V, S, T;
vector<pair<int, ll>> g[N];
// U: 1, V: 2, S: 3, T: 4
vector<ll> dist[5];
vector<int> adj[N];
ll ans = 0;
bool visted[N];

void dijk(vector<ll>& dist, int root){
    dist[root] = 0;
    priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
    pq.push({0,root});
    while(!pq.empty()){
        ll wu; int u; tie(wu,u) = pq.top(); pq.pop();
        if(wu > dist[u]) continue;
        for(auto [v, wv] : g[u]){
            if(dist[v] > dist[u] + wv){
                dist[v] = dist[u] + wv;
                pq.push({dist[v], v});
            }
        }
    }
}

void build(int u){
    visted[u] = true;
    for(auto [v, wv] : g[u]){
        if(dist[1][u] + wv + dist[2][v] == dist[1][V]){
            adj[u].push_back(v);
            if(!visted[v]) build(v);
        }
    }
}

// best[i][1] = max dist[i] -> s | best[i][2] = max dist[i] -> t
ll best[N][3];

void dfs(int u){
    visted[u] = true;
    best[u][1] = dist[3][u];
    best[u][2] = dist[4][u];
    for(int v : adj[u]){
        if(!visted[v]) dfs(v);
        best[u][1] = min(best[u][1], best[v][1]);
        best[u][2] = min(best[u][2], best[v][2]);
    }
    ans = min(ans, dist[3][u] + best[u][2]);
    ans = min(ans, dist[4][u] + best[u][1]);
}

void solve(){
    cin >> n >> m >> U >> V >> S >> T;

    for(int i = 1; i <= m; i++){
        int u, v, w; cin >> u >> v >> w;
        g[u].push_back({v,w});
        g[v].push_back({u,w});
    }

    for(int i = 1; i <= 4; i++) dist[i].resize(n+1, INF);

    dijk(dist[1], U);
    dijk(dist[2], V);
    dijk(dist[3], S);
    dijk(dist[4], T);

    build(U);

    ans = dist[3][T];

    for(int i = 1; i <= n; i++) visted[i] = false;

    dfs(U);

    cout << ans;
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "bus"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen(task".OUT", "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...