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>
- 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";
- }
- struct ed{
- int fr, to;
- ld w;
- };
- vector < vector <ed> > G;
- set <pii> Q;
- vector <int> d;
- const int inf = 2e9;
- int n,s,f;
- void Dij(int s){
- d.assign(n, inf);
- //cv(d);
- Q.insert({0, s});
- d[s] = 0;
- while (!Q.empty()){
- int v = Q.begin()->second;
- Q.erase(Q.begin());
- for (ed e: G[v]){
- int nv = e.to;
- if (d[nv] > d[v] + e.w){
- Q.insert({d[v] + e.w, nv});
- d[nv] = d[v] + e.w;
- }
- }
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n>>s>>f;
- s--;
- f--;
- G.resize(n);
- for (int i = 0; i < n; ++i){
- for (int j = 0 ;j < n;++j){
- int di;
- cin>>di;
- if (i == j) continue;
- if (di == -1) continue;
- G[i].push_back({i, j, di});
- }
- }
- Dij(s);
- if (d[f] == inf){
- cout<<-1;
- exit(0);
- }
- cout<<d[f]<<"\n";
- }
- /*
- 3 1 2
- 0 -1 2
- 3 0 -1
- -1 4 0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement