Submission #946380

#TimeUsernameProblemLanguageResultExecution timeMemory
946380vjudge1Commuter Pass (JOI18_commuter_pass)C++17
55 / 100
214 ms23260 KiB
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <cassert> #include <iomanip> #include <iostream> #include <algorithm> #include <stdio.h> #include <fstream> #include <unordered_map> #define ll long long #define int long long #define all(v) v.begin(), v.end() #define nl '\n' #define pb push_back #define sz(s) (int)(s).size() #define f first #define s second using namespace std; const ll N = 1e5 + 50, MX = 1e18; bool was[N]; void solve(){ ll n,m; cin >> n >> m; vector <pair <int, int> > g[n + 1]; ll s, t, u, v; cin >> s >> t >> u >> v; for(int i = 1; i <= m; i++){ ll a, b, x; cin >> a >> b >> x; g[a].pb({b,x}); g[b].pb({a, x}); } if(s == u){ ll ds[n + 1], dt[n + 1]; if(true){ for (int j = 1; j <= n; j++) { ds[j] = MX; } ds[s]=0; priority_queue< pair<long long, int> > st; st.push({0, s}); while(st.size() != 0) { int curv = st.top().second; st.pop(); if(was[curv]) continue; was[curv] = 1; for(auto [to, w]: g[curv]) { if(ds[to] > ds[curv] + w) { //s.erase({d[to], to}); ds[to] = ds[curv] + w; st.push({-ds[to], to}); } } } //cout << ds[v] << nl; } if(true){ for (int j = 1; j <= n; j++) { dt[j] = MX; was[j]=0; } dt[t]=0; priority_queue< pair<long long, int> > s; s.push({0, t}); while(s.size() != 0) { int curv = s.top().second; s.pop(); if(was[curv]) continue; was[curv] = 1; for(auto [to, w]: g[curv]) { //cout << to << " " << w << " " << curv << nl; if(dt[to] > dt[curv] + w) { //s.erase({d[to], to}); dt[to] = dt[curv] + w; s.push({-dt[to], to}); } } } } for(int i = 1; i <= n; i++){ //cout << i << " " << ds[i] << " " << dt[i] << nl; if(ds[i] + dt[i] == ds[t]) { for (int j = 0; j < sz(g[i]); j++) { ll x = g[i][j].f; if (dt[x] + ds[x] == ds[t]) { g[i][j].s=0; } } } } if(true){ for (int j = 1; j <= n; j++) { was[j]=0; } ds[s]=0; priority_queue< pair<long long, int> > st; st.push({0, s}); while(st.size() != 0) { int curv = st.top().second; st.pop(); if(was[curv]) continue; was[curv] = 1; for(auto [to, w]: g[curv]) { if(ds[to] > ds[curv] + w) { //s.erase({d[to], to}); ds[to] = ds[curv] + w; st.push({-ds[to], to}); } } } cout << ds[v] << nl; } } else if(n <= 300) { ll d[n+1][n + 1]; for(int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { d[i][j] = MX; } } for(int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++) { was[j] = 0; } d[i][i] = 0; priority_queue< pair<long long, int> > s; s.push({0, i}); while(s.size() != 0) { int curv = s.top().second; s.pop(); if(was[curv]) continue; was[curv] = 1; for(auto [to, w]: g[curv]) { if(i == 6){ //cout << d[i][to] << ' ' << to << " " << w << " " << d[i][v] << " " << v << nl; } if(d[i][to] > d[i][curv] + w) { //s.erase({d[to], to}); d[i][to] = d[i][curv] + w; s.push({-d[i][to], to}); } } } } ll ans = d[u][v]; //cout << d[s][t] << nl; //cout << ds[3] << " " << dt[5] << nl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (d[s][i] + d[t][j] + d[i][j] == d[s][t] || d[s][j] + d[t][i] + d[i][j] == d[s][t]) { ans = min(ans, d[u][i] + d[v][j]); } } } cout << ans << nl; } else{ ll ds[n + 1], p[n + 1], du[n + 1], dv[n + 1]; if(true){ for (int j = 1; j <= n; j++) { ds[j] = MX; } ds[s]=0; priority_queue< pair<long long, int> > st; st.push({0, s}); p[s]=0; while(st.size() != 0) { int curv = st.top().second; st.pop(); if(was[curv]) continue; was[curv] = 1; for(auto [to, w]: g[curv]) { if(ds[to] > ds[curv] + w) { //s.erase({d[to], to}); p[to] = curv; ds[to] = ds[curv] + w; st.push({-ds[to], to}); } } } //cout << ds[v] << nl; } if(true){ for (int j = 1; j <= n; j++) { was[j]=0; du[j]=MX; } du[u]=0; priority_queue< pair<long long, int> > st; st.push({0, u}); while(st.size() != 0) { int curv = st.top().second; st.pop(); if(was[curv]) continue; was[curv] = 1; for(auto [to, w]: g[curv]) { if(du[to] > du[curv] + w) { //s.erase({d[to], to}); du[to] = du[curv] + w; st.push({-du[to], to}); } } } } if(true){ for (int j = 1; j <= n; j++) { was[j]=0; dv[j]=MX; } dv[v]=0; priority_queue< pair<long long, int> > st; st.push({0, v}); while(st.size() != 0) { int curv = st.top().second; st.pop(); if(was[curv]) continue; was[curv] = 1; for(auto [to, w]: g[curv]) { if(dv[to] > dv[curv] + w) { //s.erase({d[to], to}); dv[to] = dv[curv] + w; st.push({-dv[to], to}); } } } } vector <int> path; ll x = t; while(x != 0){ path.pb(x); x = p[x]; } ll mnv=MX, mnu=MX; for(auto x:path){ //cout << x << " "; mnv = min(dv[x], mnv); mnu = min(du[x], mnu); } //cout << du[v] << nl; cout << min(mnv + mnu, dv[u]); } } signed main(){ //freopen("lca.in", "r", stdin); //freopen("lca.out", "w", stdout); ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); ll ql=1; //cin >> ql; //tst++; while(ql--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...