제출 #595337

#제출 시각아이디문제언어결과실행 시간메모리
595337WaelCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
481 ms73348 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
//#define endl '\n'
#define F first
#define S second
int const M = 1e6 + 10 , N = 1e3 + 10 , mod = 1e9 + 7;
ll T , n , m , k , cost , x , y , s , t , u , v , type , ans = 1e18 , dis[6][M] , cnt[M] , on[M] , U[M] , V[M];
int dx[] = {1 , -1 , 0 , 0} , dy[] = {0 , 0 , 1 , -1};
priority_queue<pair<ll , ll> , vector<pair<ll , ll>> , greater<pair<ll , ll>>>q;
vector<pair<ll , ll>>adj[M];
vector<ll>graph[M] , top;
bool vis[M];

inline void Dijkstra(int start) {
   q.push({0 , start});
   while(q.size()) {
    int node = q.top().S;
    int cost = q.top().F;
    q.pop();
    if(dis[type][node] || (node == start && cost)) continue;
    dis[type][node] = cost;
    for(auto next : adj[node]) q.push({cost + next.S , next.F});
   }
}

inline void Bfs() {
   queue<ll>Q;
   Q.push(s);
   while(Q.size()) {
    int node = Q.front();
    Q.pop();
    top.push_back(node);
    for(auto next : graph[node]) {
        --cnt[next];
        if(cnt[next] == 0) Q.push(next);
    }
   }
}

main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

    cin >> n >> m >> s >> t >> u >> v;
    for(int i = 1 ; i <= m ; ++i) {
        cin >> x >> y >> cost;
        adj[x].push_back({y , cost});
        adj[y].push_back({x , cost});
    }
    type = 1; Dijkstra(u);
    type = 2; Dijkstra(v);
    type = 3; Dijkstra(s);
    type = 4; Dijkstra(t);

    for(int node = 1 ; node <= n ; ++node) if(dis[3][node] + dis[4][node] == dis[3][t]) on[node] = 1;

    for(int node = 1 ; node <= n ; ++node)
        if(on[node])
            for(auto next : adj[node])
                if(on[next.F] && dis[3][node] + next.S == dis[3][next.F]) {
                    ++cnt[next.F];
                    graph[node].push_back(next.F);
                }

    Bfs();
    for(int i = 1 ; i <= n ; ++i) U[i] = V[i] = 1e18;
    ans = dis[1][v];
    for(auto node : top) {
        ans = min(ans , dis[1][node] + V[node]);
        ans = min(ans , dis[2][node] + U[node]);
        U[node] = min(U[node] , dis[1][node]);
        V[node] = min(V[node] , dis[2][node]);
        for(auto next : graph[node]) U[next] = min(U[next] , U[node]) , V[next] = min(V[next] , V[node]);
    }
    cout << ans << endl;
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

commuter_pass.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   42 | main() {
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...