제출 #701849

#제출 시각아이디문제언어결과실행 시간메모리
701849mychecksedadCommuter Pass (JOI18_commuter_pass)C++17
31 / 100
341 ms53228 KiB
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1e6+100, M = 1e5+10, K = 20;
 
 
int n, m, s, t, x, y;
ll d[N], dp[N][3];
vector<array<ll, 3>> g[N];
vector<bool> vis;
vector<array<bool, 3>> vis2;
vector<array<int, 2>> good;
 
void solve(){
  cin >> n >> m >> s >> t >> x >> y;
  for(int i = 0; i < m; ++i){
    int u, v, w; cin >> u >> v >> w;
    g[u].pb({v, w, i});
    g[v].pb({u, w, i});
  }
  vis.resize(n + 1);
  good.resize(m);

  //first dijkstra
  priority_queue<pair<ll, int>> q;
  q.push({0, s});
  for(int i = 1; i <= n; ++i) d[i] = i == s ? 0 : LONG_LONG_MAX;
  while(!q.empty()){
    int v = q.top().second; q.pop();
    if(vis[v]) continue;
    vis[v] = 1;
    for(auto U: g[v]){
      ll u = U[0], w = U[1];
      if(!vis[u] && d[u] > d[v] + w){
        d[u] = d[v] + w;
        q.push({-d[u], u});
      }
    }
  }
  //path recovering
  queue<int> Q;
  Q.push(t);
  fill(all(vis), 0);
  vis[t] = 1;
  while(!Q.empty()){
    int v = Q.front(); Q.pop();
    for(auto U: g[v]){
      int u = U[0], w = U[1];
      if(d[u] == d[v] - w){
        good[U[2]] = {v, u};
        if(!vis[u]){
          vis[u] = 1;
          Q.push(u);
        }
      }
    }
  }
  //last dijkstra
  priority_queue<array<ll, 4>> q2;

  ll ans = LONG_LONG_MAX;

  for(int i = 1; i <= n; ++i) dp[i][0] = dp[i][1] = dp[i][2] = LONG_LONG_MAX;

  dp[x][0] = 0;
  q2.push({0, x, 0, 0}); // cost, node, type, direction
  vis2.resize(n + 1, {0, 0, 0});
  while(!q2.empty()){
    int v = q2.top()[1], tp = q2.top()[2], dir = q2.top()[3]; q2.pop();
    if(vis2[v][tp]) continue;
    vis2[v][tp] = 1;
    for(auto U: g[v]){
      int u = U[0], w = U[1];
      if(good[U[2]][0] == u && good[U[2]][1] == v && tp != 2 && dir != 1){
        if(!vis2[u][1] && dp[u][1] > dp[v][tp]){
          dp[u][1] = dp[v][tp];
          q2.push({-dp[u][1], u, 1, 2});
        }
      }else if(good[U[2]][0] == v && good[U[2]][1] == u && tp != 2 && dir != 2){
        if(!vis2[u][1] && dp[u][1] > dp[v][tp]){
          dp[u][1] = dp[v][tp];
          q2.push({-dp[u][1], u, 1, 1});
        }
      }else{
        int tt = tp >= 1 ? 2 : 0;
        if(!vis2[u][tt] && dp[u][tt] > dp[v][tp] + w){
          dp[u][tt] = dp[v][tp] + w;
          q2.push({-dp[u][tt], u, tt, dir});
        }
      }
    }
  }
  ans = min({dp[y][0], dp[y][1], dp[y][2]});
  cout << ans;
}
 
 
int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int T = 1, aa;
  // cin >> T;aa=T;
  while(T--){
    // cout << "Case #" << aa-T << ": ";
    solve();
    cout << '\n';
  }
  return 0;
 
}

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

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:106:14: warning: unused variable 'aa' [-Wunused-variable]
  106 |   int T = 1, aa;
      |              ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...