Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<pair<int, int>> graph[2001];
- int distances[2001];
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- freopen("pathmgep.in", "r", stdin);
- freopen("pathmgep.out", "w", stdout);
- int n, start, finish;
- cin >> n >> start >> finish;
- start--;
- finish--;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- int weight;
- cin >> weight;
- if (weight != 0 && weight != -1) {
- graph[i].push_back({j, weight});
- }
- }
- }
- for (int i = 0; i < n; i++) {
- distances[i] = 1e9 + 1;
- }
- distances[start] = 0;
- priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
- q.push({start, 0});
- while (!q.empty()) {
- pair<int, int> top = q.top();
- q.pop();
- int v = top.first, dst = top.second;
- if (distances[v] < dst) {
- continue;
- }
- for (pair<int, int> e: graph[v]) {
- int u = e.first, len_vtou = e.second;
- int new_dst = dst + len_vtou;
- if (new_dst < distances[u]) {
- distances[u] = new_dst;
- q.push({u, new_dst});
- }
- }
- }
- if (distances[finish] != 1e9 + 1) {
- cout << distances[finish] << endl;
- } else cout << -1 << endl;
- return 0;
- }
- /*
- 3 1 2
- 0 -1 2
- 3 0 -1
- -1 4 0
- 3 1 2
- 0 -1 2
- 3 0 -1
- -1 -1 0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement