Submission #959202

#TimeUsernameProblemLanguageResultExecution timeMemory
959202AbitoRailway Trip (JOI17_railway_trip)C++17
5 / 100
1 ms604 KiB
#include <bits/stdc++.h> #define F first #define S second #define pb push_back #define ppb pop_back #define ep insert #define endl '\n' #define elif else if #define pow pwr #define sqrt sqrtt #define int long long #define ll long long typedef unsigned long long ull; using namespace std; const int N=105; int a[N],n,q,k,dis[N]; bool vis[N]; vector<pair<int,int>> adj[N]; void dijkstra(int s){ for (int i=0;i<N;i++) dis[i]=INT_MAX,vis[i]=0; dis[s]=0; priority_queue<pair<int,int>> pq; pq.push({0,s}); while (!pq.empty()){ int x=pq.top().S; pq.pop(); if (vis[x]) continue; vis[x]=1; for (auto u:adj[x]){ if (dis[u.F]>dis[x]+u.S){ dis[u.F]=dis[x]+u.S; pq.push({-dis[u.F],u.F}); } } }return; } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); cin>>n>>k>>q; for (int i=1;i<=n;i++) cin>>a[i]; for (int i=1;i<=n;i++){ for (int j=i+1;j<=n;j++){ if (a[j]<a[i]) continue; adj[i].pb({j,1}); adj[j].pb({i,1}); break; } for (int j=i-1;j>0;j--){ if (a[j]<a[i]) continue; adj[i].pb({j,1}); adj[j].pb({i,1}); break; } } while (q--){ int x,y; cin>>x>>y; dijkstra(x); cout<<dis[y]-1<<endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...