Submission
Status:
(PPPP)(PPPPPP)(PPPPPPTTTT)
Score: 80
User: hmmm
Problemset: ย้อนศร
Language: cpp
Time: 0.796 second
Submitted On: 2025-01-09 17:04:32
#include<bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using ppi=array<int,3>;
const int N=1e5+5;
vector<pii> g[N];
int dis[2][N];
int vis[2][N];
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int n,m;
cin >> n >> m;
for(int i=1;i<=m;i++){
int u,v;
cin >> u >> v;
g[u].push_back({v,0});
g[v].push_back({u,1});
}
int t;
cin >> t;
while(t--){
int u,v;
cin >> u >> v;
priority_queue<ppi,vector<ppi>,greater<ppi>> q;
for(int i=1;i<=n;i++){
dis[0][i]=dis[1][i]=1e9;
vis[0][i]=vis[1][i]=false;
}
q.push({dis[0][u]=0,u,0});
while(!q.empty()){
auto l=q.top()[0];
auto x=q.top()[1];
auto st=q.top()[2];
q.pop();
if(x==v){
cout << l << "\n";
break;
}
// cout << l << ' ' << x << ' ' << st << "\n";
if(vis[st][x]) continue;
vis[st][x]=true;
for(auto e:g[x]){
auto xx=e.first;
auto ll=e.second;
// cout << xx << "-\n";
// cout << dis[st][xx] << ' ' << dis[st][xx]+ll << '\n';
int y=max(st,ll);
if(dis[y][xx]>dis[st][x]+ll && !vis[y][xx]){
dis[y][xx]=dis[st][x]+ll;
q.push({dis[y][xx],xx,y});
// cout << "a\n";
}
}
}
}
}