제출 #70743

#제출 시각아이디문제언어결과실행 시간메모리
70743someone_aaCommuter Pass (JOI18_commuter_pass)C++17
15 / 100
2056 ms263168 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
ll dist[maxn][4], n, m, s, t, u, v;
ll dp[maxn][2];
bool visited[maxn];
vector<pair<int,int> > g[maxn];

/*
    dist[i][0] - dist to i from u
    dist[i][1] - dist to i from v
    dist[i][2] - dist to i from s
    dist[i][3] - dist to i from t
*/

void dijkstra(int start, int x) {
    memset(visited,false,sizeof(visited));
    for(int i=0;i<=n;i++) {
        dist[i][x] = 10000000LL * INT_MAX;
    }
    dist[start][x] = 0LL;
    //visited[start] = true;

    priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
    pq.push(mp(0, start));

    while(!pq.empty()) {
        int curr = pq.top().second;
        pq.pop();
        if(visited[curr]) continue;
        visited[curr] = true;
        for(auto i:g[curr]) {
            if(dist[curr][x] + i.second < dist[i.first][x] && !visited[i.first]) {
                dist[i.first][x] = dist[curr][x] + i.second;
                pq.push(mp(dist[i.first][x], i.first));
            }
        }
    }
}

void get_dp(int start, int x) {
    memset(visited,false,sizeof(visited));
    for(int i=0;i<=n;i++) {
        dp[i][x] = dist[i][1];
    }

    priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
    pq.push(mp(0, start));

    int fx, sx, ffx;
    if(x == 0) {
        fx = 2;
        sx = 3;
        ffx = t;
    }
    else {
        fx = 3;
        sx = 2;
        ffx = s;
    }

    while(!pq.empty()) {
        int curr = pq.top().second;
        pq.pop();
        visited[curr] = true;
        for(auto i:g[curr]) {
            // check if this edge lies on some shortest path from s to v
            if(dist[curr][fx] + dist[i.first][sx] + i.second == dist[ffx][fx] && !visited[i.first]) {
                dp[i.first][x] = min(dp[curr][x], dp[i.first][x]);
                pq.push(mp(dist[i.first][fx], i.first));
            }
        }
    }
}

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

    int x, y, z;
    for(int i=0;i<m;i++) {
        cin>>x>>y>>z;
        g[x].pb(mp(y,z));
        g[y].pb(mp(x,z));
    }

    dijkstra(u, 0);
    dijkstra(v, 1);
    dijkstra(s, 2);
    dijkstra(t, 3);

    get_dp(s, 0);
    get_dp(t, 1);

    ll result = LLONG_MAX;

    for(int i=1;i<=n;i++) {
        result = min(result, dist[i][0] + min(dp[i][0], dp[i][1]));
    }
    cout<<result;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...