Блог пользователя apjcoder123

Автор apjcoder123, история, 3 года назад, По-английски

Problem Name: Planets

Problem link: https://codeforces.net/contest/229/problem/B

My approach and doubt: If we apply the modified Dijkstra like said in the editorial, and if the same vertex is visited many times in the min heap, then due to this then the same list of travelers may be traversed multiple times, which may result into a TLE. but the editorial says that the same list of travelers wont be traversed multiple times, Can somebody explain me why and where I am going wrong ?

Thanks in advance.

My Accepted Code:

#include <bits/stdc++.h>
using namespace std;
#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define lli long long int
#define ll long long
#define pll pair<long,long>

ll n,m;
ll N=1e5+100;
ll INF=1e18+1000;
vector<vector<pll>> adj(N,vector<pll>());
vector<vector<ll>> trv(N,vector<ll>());
vector<ll> dist(N,INF);

void dijkstra(ll src)
{
    priority_queue<pll,vector<pll>,greater<pll>> min_heap;
    dist[src]=0;
    min_heap.push({dist[src],src});
    while(!min_heap.empty())
    {
        ll curr_n=min_heap.top().second;
        ll curr_d=min_heap.top().first;
        min_heap.pop();
        
        ll nn=trv[curr_n].size();
        ll act_d=-1;
        
        if(nn==0) act_d=curr_d;
        else if(curr_d<trv[curr_n][0]) act_d=curr_d;
        else 
        {
            for(ll i=0;i<nn-1;i++)
            {
                if(trv[curr_n][i]+1<trv[curr_n][i+1] && curr_d<trv[curr_n][i+1])
                { act_d=max(trv[curr_n][i]+1,curr_d); break; }
            }
        }
        
        if(act_d==-1) act_d=max(trv[curr_n].back()+1,curr_d);
        
        for(auto opt:adj[curr_n])
        {
            ll next_n=opt.first;
            ll next_d=opt.second;
            
            if(act_d+next_d<dist[next_n])
            {
                dist[next_n]=act_d+next_d;
                min_heap.push({dist[next_n],next_n});
            }
        }
    }
    
    return;
}

void solve()
{
    cin>>n>>m;
    for(ll i=0;i<m;i++)
    {
        ll u,v,w; cin>>u>>v>>w;
        adj[u].push_back({v,w});
        adj[v].push_back({u,w});
    }
    
    for(ll i=1;i<=n;i++)
    {
        ll cnt; cin>>cnt;
        
        while(cnt--)
        { ll x; cin>>x; trv[i].push_back(x); }
    }
    
    dijkstra(1);
    
    cout<<(dist[n]==INF ? -1 : dist[n])<<'\n';
    
    return;
}

int main()
{
    fastio();
    int t=1;
    // cin>>t;
    while(t--) solve();

    return 0;
}
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

and if the same vertex is visited many times in the min heap
But you never visit same vertex in the min heap. Because vertexed are visited in "global time-order"