Submission

Status:
(PPPP)(PPPPPP)(PPPPPPTTPT)

Score: 80

User: njoop

Problemset: ย้อนศร

Language: cpp

Time: 1.097 second

Submitted On: 2024-12-14 21:02:29

#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});
    dis[st] = 0;
    while(pq.size()) {
        di = pq.top().first;
        no = pq.top().second;
        pq.pop();
        if(di > dis[no]) continue;
        for(auto i: g[no]) {
            nd = di+i.first;
            if(nd < dis[i.second]) {
                dis[i.second] = nd;
                pq.push({nd, 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;
}