Submission #989958

#TimeUsernameProblemLanguageResultExecution timeMemory
989958ranmkCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
368 ms33644 KiB
#include <bits/stdc++.h> using namespace std; typedef long long int ll; /*---------------------------------------------------------------------------------------------------------------------------*/ ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);} ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;} void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3 ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);} bool revsort(ll a, ll b) {return a > b;} void swap(int &x, int &y) {int temp = x; x = y; y = temp;} ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;} void google(int t) {cout << "Case #" << t << ": ";} vector <ll> binary_rep (ll n){ vector <ll> ans; for(int i=0; i<31; i++){ if(n & (1<<i)){ ans.push_back((1<<i)); } } return ans; } vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;} ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N)) // scanf("%d%d", &l[i], &r[i]) #define endl "\n" // priority_queue <int, vector<int>, greater<int>> pq; /*---------------------------------------------------------------------------------------------------------------------------*/ const ll MOD = 1e9 + 7; ll N = 1e5 + 2; ll M = 2e5 + 2; ll ans = LLONG_MAX/2; vector <vector <pair<ll, ll>>> adj(N); vector <bool> visited (N, false); vector <ll> du(N, LLONG_MAX/2), dv(N, LLONG_MAX/2), ds(N, LLONG_MAX/2), dpU(N, LLONG_MAX/2), dpV(N, LLONG_MAX/2); void dijkstra1(ll index, vector <ll>& arr){ priority_queue <pair <ll, ll>> pq; pq.push(make_pair(0, index)); fill(visited.begin(), visited.end(), false); while(!pq.empty()){ pair <ll, ll> p = pq.top(); pq.pop(); if(visited[p.second]==false){ arr[p.second] = -p.first; visited[p.second] = true; for(auto ad: adj[p.second]) pq.push(make_pair(p.first - ad.second, ad.first)); } } } void dijkstra2(ll start, ll end){ priority_queue <pair <ll, pair <ll, ll>>> pq; dpU[0] = LLONG_MAX/2; dpV[0] = LLONG_MAX/2; fill(dpU.begin(), dpU.end(), LLONG_MAX/2); fill(dpV.begin(), dpV.end(), LLONG_MAX/2); fill(ds.begin(), ds.end(), LLONG_MAX/2); fill(visited.begin(), visited.end(), false); pq.push({0, {start, 0}}); while(!pq.empty()){ auto p = pq.top(); pq.pop(); if(visited[p.second.first]==false){ ds[p.second.first] = -p.first; visited[p.second.first] = true; dpU[p.second.first] = min(dpU[p.second.second], du[p.second.first]); dpV[p.second.first] = min(dpV[p.second.second], dv[p.second.first]); for(auto nb: adj[p.second.first]) pq.push({p.first - nb.second, {nb.first, p.second.first}}); } else if(ds[p.second.first] == -p.first){ if(min(dpU[p.second.second], du[p.second.first]) + min(dpV[p.second.second], dv[p.second.first]) < dpU[p.second.first] + dpV[p.second.first]){ dpU[p.second.first] = min(dpU[p.second.second], du[p.second.first]); dpV[p.second.first] = min(dpV[p.second.second], dv[p.second.first]); } } // ye dono conditions saath me hi honge priority queue me, agar ds == -p.f hai, to mtlb usse just pehle // ye value set hui hai since its a priority queue } ans = min(ans, dpU[end] + dpV[end]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // #ifndef ONLINE_JUDGE // freopen("cbarn2.in", "r", stdin); // freopen("cbarn2.out", "w", stdout); // #endif int n, m; cin>>n>>m; int s, t, u, v; cin>>s>>t>>u>>v; for(int i=0; i<m; i++){ int a, b, c; cin>>a>>b>>c; adj[a].push_back(make_pair(b, c)); adj[b].push_back(make_pair(a, c)); } dijkstra1(u, du); dijkstra1(v, dv); ans = du[v]; dijkstra2(s, t); dijkstra2(t, s); cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...