Submission #1255205

#TimeUsernameProblemLanguageResultExecution timeMemory
1255205thanhlaihoccodeCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
636 ms26144 KiB
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define FILE "task"
#define pb push_back
#define endl '\n'
#define fi first
#define se second
#define ii pair<int, int>
#define FOR(i, l, r) for (int i = l; i <= r; i++)
#define FOD(i, l, r) for (int i = l; i >= r; i--)
#define ex exit(0)
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ff fi.fi
//#define fs fi.se
#define ss se.se
#define sf se.fi
#define MASK(i) (1LL << i)
#define BASE 256
int const N = 1e5 + 5, mod = 1e9 + 7, M = 2e5 + 5, K = 34;
int n, m, x, y, s, t, in[N], flag[N];
int mark[N];
long long fs[N], ft[N], dp[N], ans = 1e18;
vector<ii> e[M];
vector<pair<ii, int>> c;
vector<ii> e2[M];
vector<int> topo;
queue<int> q;
void dijk(int s, long long f[N])
{
  FOR(i, 1, n) f[i] = 1e18;
  f[s] = 0;
  priority_queue<ii, vector<ii>, greater<ii>> pq;
  pq.push({0, s});
  
  while(!pq.empty())
  {
    int u = pq.top().se;
    int cost = pq.top().fi;
    pq.pop();
   
    if(cost > f[u]) continue;

    for(auto x : e[u])
    {
      int v = x.fi, w = x.se;
      if(f[v] > f[u] + w)
      {
        f[v] = f[u] + w;
        pq.push({f[v], v});
      }
    }
  }
}

void init()
{
    cin >> n >> m >> s >> t >> x >> y;
    FOR(i, 1, m)
    {
        int x, y, w; cin >> x >> y >> w;
        e[x].pb({y, w});
        e[y].pb({x, w});
        c.pb({{x, y}, w});
    }

    FOR(i, 1, n)
    {
      sort(e[i].begin(), e[i].end());
      e[i].erase(unique(e[i].begin(), e[i].end()), e[i].end());
    }

}

void sub2()
{
  if(x == y)
  {
    cout << 0; ex;
  }
    dijk(s, fs);
    dijk(t, ft);
  
    FOR(u, 1, n) {
        for (auto& edge : e[u]) {
            int v = edge.fi;
            int w = edge.se;
            if (fs[u] + w + ft[v] == fs[t]) {
                e2[u].pb({v, w});
                in[v]++; flag[u]++; flag[v]++;
            }
        }
    }

   // FOR(i, 1, n) cout << in[i] << endl;
    // FOR(i, 1, n)
    // {
    //   for(auto x : e2[i]) cout << i << " " << x.fi << " " << x.se;
    //   cout << endl;
    // }

    int topocnt = 0;
    
    FOR(i, 1, n) if(!in[i] && flag[i] >= 1) q.push(i), mark[i] = ++topocnt;

    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        topo.pb(u);
        for(auto x : e2[u])
        {
          int v = x.fi, w = x.se;
            in[v]--;
            if(!in[v]) q.push(v), mark[v] = ++topocnt;
        }
    }

    // for(auto x : topo) cout << x << " ";
    // cout << endl;

    if(mark[x] > mark[y] && mark[x] && mark[y]) swap(x, y);

    dijk(x, fs);
    dijk(y, ft);

    FOR(i, 1, n) dp[i] = 1e18;

    FOD(i, topo.size() - 1, 0)
    {
      int u = topo[i];
      dp[u] = ft[u];
      for(auto x : e2[u])
      {
        int v = x.fi, w = x.se;
        dp[u] = min(dp[u], dp[v]);
      }
    }

    for(auto u : topo)
    {
      ans = min(ans, fs[u] + dp[u]);
  //    cout << fx[u] + dp[u] << " " << fx[u] << " " << dp[u] << endl;
    }

    FOR(i, 1, n) dp[i] = 1e18;

    FOD(i, topo.size() - 1, 0)
    {
      int u = topo[i];
      dp[u] = fs[u];
      for(auto x : e2[u])
      {
        int v = x.fi, w = x.se;
        dp[u] = min(dp[u], dp[v]);
      }
    }

     for(auto u : topo)
    {
      ans = min(ans, ft[u] + dp[u]);
  //    cout << fx[u] + dp[u] << " " << fx[u] << " " << dp[u] << endl;
    }
    cout << min(ans, fs[y]);
}
signed main()
{
if (fopen(FILE ".inp", "r"))
  {
    freopen(FILE ".inp", "r", stdin);
    freopen(FILE ".out", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
   init();
   sub2();
}

/*
▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░
 ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
 ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
 ░▒▓█▓▒░   ░▒▓████████▓▒░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░
 ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
 ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
 ░▒▓█▓▒░   ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
                                                                 */

Compilation message (stderr)

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