Submission #565664

#TimeUsernameProblemLanguageResultExecution timeMemory
565664AbdullahMWEvacuation plan (IZhO18_plan)C++14
22 / 100
4089 ms28448 KiB
#include <bits/stdc++.h>

#define all(vec) vec.begin(), vec.end()
#define ll long long
#define db double
#define pb push_back
#define pf push_front
#define newl "\n"
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define f first
#define s second
#define MOD 1000000007

using namespace std;

vector <pair <ll, ll>> gr[100005];
unordered_map <ll, ll> fin, dist;
unordered_map <ll, bool> nuc, vis;


void near(ll beg, ll p)
{
    priority_queue <pair <ll, ll>, vector <pair <ll, ll>>, greater <pair <ll, ll>>> pq;
    pq.push({0, beg});
    if (vis.size()) vis.clear();
    
    while (pq.size())
    {
        ll c = pq.top().s, len = pq.top().f;
        pq.pop();
        if (!vis[c])
        {
            if (nuc[c]) fin[p] = min(fin[p], len);
            vis[c] = true;
            for (auto v : gr[c])
            {
                pq.push({len + v.f, v.s});
            }
        }
    }
}


ll dj(ll beg, ll nd)
{
    priority_queue <pair <ll, ll>> d;
    d.push({fin[beg], beg});
    if (vis.size()) vis.clear();
    
    while (d.size())
    {
        ll c = d.top().s, len = d.top().f;
        d.pop();
        if (!vis[c])
        {
            if (c == nd) return len;
            vis[c] = true;
            for (auto v : gr[c])
            {
                d.push({min(len, fin[v.s]), v.s});
            }
        }
    }
}

int main()
{
    fast
    //setIO("");
    
    //freopen("filename.in", "r", stdin);
    //freopen("filename.out", "w", stdout);
  
    ll n, m; cin >> n >> m;
    for (ll i = 1; i <= m; i++)
    {
        ll x, y, len;
        
        cin >> x >> y >> len;
        gr[x].pb({len, y});
        gr[y].pb({len, x});
    }
    
    ll k; cin >> k;
    for (ll i = 1; i <= k; i++)
    {
        ll idx; cin >> idx;
        nuc[idx] = true;
    }
    
    for (ll i = 1; i <= n; i++)
    {    
        fin[i] = 1e18;
        if (!nuc[i]) near(i, i);
        
    }
    
    for (ll i = 1; i <= n; i++)
    {
        if (nuc[i]) fin[i] = 0;
    }
    
    
    //cout << newl;
    ll q; cin >> q;
    while (q--)
    {
        ll x, y; cin >> x >> y;
        cout << dj(x, y) << newl;
    }
    
    /*cout << newl;
    for (ll i = 1; i <= n; i++)
    {
        if (!nuc[i]) cout << "City: " << i << " Dist: " << fin[i] << newl;
    }*/
    
    
}
    

Compilation message (stderr)

plan.cpp: In function 'long long int dj(long long int, long long int)':
plan.cpp:46:36: warning: control reaches end of non-void function [-Wreturn-type]
   46 |     priority_queue <pair <ll, ll>> d;
      |                                    ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...