Submission
Status:
(PPPP)(PPPPPP)(TTTTTTTTTT)
Score: 80
User: njoop
Problemset: ย้อนศร
Language: cpp
Time: 0.798 second
Submitted On: 2024-12-14 21:27:16
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
vector<pii> g[100010];
int dis[100010], n, m, a, b, c, di, no, st, en, nd, q;
priority_queue<pii, vector<pii>, greater<pii>> pq;
int dijkstra() {
for(int i=1; i<=n; i++) {
dis[i] = 1e9;
}
pq.push({0, st});
while(pq.size()) {
di = pq.top().first;
no = pq.top().second;
pq.pop();
if(di >= dis[no]) continue;
dis[no] = di;
for(auto i: g[no]) {
pq.push({di+i.first, i.second});
}
}
return dis[en];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
while(m--) {
cin >> a >> b;
g[a].push_back({0, b});
g[b].push_back({1, a});
}
cin >> q;
while(q--) {
cin >> st >> en;
cout << dijkstra() << "\n";
}
return 0;
}