Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define INF 100000000000000000
- long long N, A, B, i, j, k, a, b, c, w, dist[10010], proc[10010], source, dest;
- vector<vector<pair<long long, long long>>> adj;
- priority_queue<pair<long long, long long>> q;
- int main() {
- ifstream cin("input.txt");
- ofstream cout("output.txt");
- cin >> N >> A >> B;
- source = 1;
- dest = N;
- for (i = 0; i <= A + B; i++) {
- adj.push_back({});
- }
- for (i = 0; i < A; i++) {
- cin >> a >> b;
- adj[a].push_back({b, 0});
- adj[b].push_back({a, 0});
- }
- for (i = 0; i < B; i++) {
- cin >> a >> b;
- adj[a].push_back({b, 1});
- adj[b].push_back({a, 1});
- }
- for (i = 1; i <= N; i++) {
- dist[i] = INF;
- proc[i] = 0;
- }
- dist[source] = 0;
- q.push({0, source});
- while (!q.empty()) {
- a = q.top().second;
- q.pop();
- if (proc[a] == 1) {
- continue;
- }
- proc[a] = 1;
- for (auto u : adj[a]) {
- b = u.first;
- w = u.second;
- if (dist[a] + w < dist[b]) {
- dist[b] = dist[a] + w;
- q.push({-dist[b], b});
- }
- }
- }
- cout << dist[dest];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement