제출 #1192057

#제출 시각아이디문제언어결과실행 시간메모리
1192057jbn8Commuter Pass (JOI18_commuter_pass)C++20
15 / 100
375 ms40252 KiB
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <functional>
#include <cassert>
using namespace std;
#define int long long
int N, M;
int S, T;
int U, V;
#define assert_in(a)assert(a < N);assert(a >= 0);
vector<map<int, int>> G;
class Node{
public:
    int cost, node, from;
    const bool operator>(const Node& o) const{
        return cost > o.cost;
    }
    const bool operator>=(const Node& o) const{
        return cost >= o.cost;
    }
    const bool operator<=(const Node& o) const{
        return cost <= o.cost;
    }
    const bool operator<(const Node& o) const{
        return cost < o.cost;
    }
};
vector<bool> is_in_solut(vector<vector<int>> G, int node){
    vector<bool> visited(N, false);
    function<void(int)> dfs;
    dfs = [&](int node){
        if(visited[node])return;
        visited[node] = true;
        for(int next_node:G[node]){
            if(next_node == -1)continue;
            if(!visited[next_node])
                dfs(next_node);
        }
    };
    dfs(node);
    return visited;
}
signed main(){
    cin >> N >> M;
    cin >> S >> T;
    cin >> U >> V;
    S--, U--, T--, V--;
    G = vector<map<int, int>>(N);
    assert_in(S);assert_in(T);
    assert_in(U);assert_in(V);
    for(int i=0; i<M; i++){
        int node0, node1, cost;
        cin >> node0 >> node1 >> cost;
        node0--, node1--;
        assert_in(node0);assert_in(node1);
        G[node0][node1] = cost;
        G[node1][node0] = cost;
    }
    priority_queue<Node, vector<Node>, greater<Node>> Q;
    vector<int> S_T(N, -1);
    vector<vector<int>> S_Tfrom(N);
    Q.push({0, S, -1});
    while(!Q.empty()){
        Node cur=Q.top();
        Q.pop();
        assert_in(cur.node);
        //cout << cur.node << ' ' << cur.from << ' ' << cur.cost << '\n';
        if(S_T[cur.node] != -1){
            if(S_T[cur.node] == cur.cost)
                S_Tfrom[cur.node].push_back(cur.from);
            continue;
        }
        //if(cur.node == T)break;
        S_T[cur.node] = cur.cost;
        S_Tfrom[cur.node].push_back(cur.from);
        for(auto A:G[cur.node]){
            if(S_T[A.first] == -1)
                Q.push({cur.cost+A.second, A.first, cur.node});
        }
    }
    int node=T;
    while(node != -1){
        int next_node = S_Tfrom[node][0];
        if(next_node == -1)break;
        G[node][next_node] = 0;
        G[next_node][node] = 0;
        node = next_node;
    }
    priority_queue<Node, vector<Node>, greater<Node>> Q2;
    vector<int> U_V(N, -1);
    Q.push({0, U, -1});
    while(!Q.empty()){
        Node cur=Q.top();
        Q.pop();
        assert_in(cur.node);
        //cout << cur.node << ' ' << cur.from << ' ' << cur.cost << '\n';
        if(U_V[cur.node] != -1)
            continue;
        if(cur.node == V){
            cout << cur.cost << '\n';
            break;
        };
        U_V[cur.node] = cur.cost;
        for(auto A:G[cur.node]){
            if(U_V[A.first] == -1)
                Q.push({cur.cost+A.second, A.first, cur.node});
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...