Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- const int inf = 2e9;
- struct ed{
- int fr,to,w;
- };
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int n, start, finish;
- cin>>n>>start>>finish;
- start--; finish--;
- vector <int> d(n, inf);
- vector <vector <pii> > G(n);
- for (int i = 0; i < n; ++i){
- for (int j = 0; j < n;++j){
- int x; cin>>x;
- if (x == -1 || i == j) continue;
- pii e = {j, x};
- G[i].push_back(e);
- }
- }
- d[start]=0;
- set <pii> Q;//расстояние, в-на
- Q.insert({0, start});
- //e = {v, l}
- while (!Q.empty()){
- int v = Q.begin()->second;
- Q.erase(Q.begin());
- for (pii e: G[v]){
- //if (d[e.first] != inf) continue;
- if (d[e.first] > d[v] + e.second){
- Q.erase({d[e.first], e.first});
- d[e.first] = d[v] + e.second;
- Q.insert({d[e.first], e.first});
- }
- }
- }
- if (d[finish] == inf){
- cout<<-1;
- exit(0);
- }
- cout<<d[finish];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement