3
3
#include < queue>
4
4
#include < cmath>
5
5
#define INDEX 1 // set this to 1 if graph input starts with 1 as first node.
6
- #define UNDIRECTED // define this to process the input for undirected graph test case
6
+ // #define UNDIRECTED // define this to process the input for undirected graph test case
7
7
using std::vector;
8
8
using std::queue;
9
9
using std::pair;
@@ -17,53 +17,57 @@ class Compare
17
17
}
18
18
};
19
19
20
- int distance (vector<vector<int > > &adj, vector<vector<double > > &cost, int s, int t) {
21
- // write your code her
22
- vector<double > dist (adj.size (), INFINITY);
23
- priority_queue<pair<int , int >, vector<pair<int , int >>, Compare> p;
24
- p.push (std::make_pair (s, 0 ));
25
- dist[s] = 0 ;
26
- vector<bool > visited (adj.size (), 0 );
27
- while (!p.empty ())
28
- {
29
- int n = p.top ().first ;
30
- p.pop ();
31
- if (visited[n])
32
- continue ;
33
- for (int i =0 ; i< adj[n].size (); i++)
34
- {
35
- if (dist[adj[n][i]] > (dist[n] + cost[n][i])) // Relax
36
- {
37
- dist[adj[n][i]] = (dist[n] + cost[n][i]);
38
- p.push (std::make_pair (adj[n][i], dist[adj[n][i]]));
39
- }
40
- }
41
- visited[n] = true ;
42
- }
43
- if (dist[t]!=INFINITY)
44
- return dist[t];
45
- else
46
- return -1 ;
47
- }
20
+ int distance (vector<vector<int > > &adj, vector<vector<double > > &cost, int s,
21
+ int t)
22
+ {
23
+ // write your code her
24
+ vector<double > dist (adj.size (), INFINITY);
25
+ priority_queue<pair<int , int >, vector<pair<int , int >>, Compare> p;
26
+ p.push (std::make_pair (s, 0 ));
27
+ dist[s] = 0 ;
28
+ vector<bool > visited (adj.size (), 0 );
29
+ while (!p.empty ())
30
+ {
31
+ int n = p.top ().first ;
32
+ p.pop ();
33
+ if (visited[n])
34
+ continue ;
35
+ for (int i = 0 ; i < adj[n].size (); i++)
36
+ {
37
+ if (dist[adj[n][i]] > (dist[n] + cost[n][i])) // Relax
38
+ {
39
+ dist[adj[n][i]] = (dist[n] + cost[n][i]);
40
+ p.push (std::make_pair (adj[n][i], dist[adj[n][i]]));
41
+ }
42
+ }
43
+ visited[n] = true ;
44
+ }
45
+ if (dist[t] != INFINITY)
46
+ return dist[t];
47
+ else
48
+ return -1 ;
49
+ }
48
50
49
- int main () {
50
- int n, m;
51
- std::cin >> n >> m;
52
- vector<vector<int > > adj (n, vector<int >());
53
- vector<vector<double > > cost (n, vector<double >());
54
- for (int i = 0 ; i < m; i++) {
55
- int x, y, w;
56
- std::cin >> x >> y >> w;
57
- adj[x - INDEX].push_back (y - INDEX);
58
- cost[x - INDEX].push_back (w);
51
+ int main ()
52
+ {
53
+ int n, m;
54
+ std::cin >> n >> m;
55
+ vector < vector<int > > adj (n, vector<int >());
56
+ vector < vector<double > > cost (n, vector<double >());
57
+ for (int i = 0 ; i < m; i++)
58
+ {
59
+ int x, y, w;
60
+ std::cin >> x >> y >> w;
61
+ adj[x - INDEX].push_back (y - INDEX);
62
+ cost[x - INDEX].push_back (w);
59
63
#ifdef UNDIRECTED
60
- adj[y - INDEX].push_back (x - INDEX);
61
- cost[y - INDEX].push_back (w);
64
+ adj[y - INDEX].push_back (x - INDEX);
65
+ cost[y - INDEX].push_back (w);
62
66
#endif
63
- }
64
- int s, t;
65
- std::cin >> s >> t;
66
- s = s - INDEX;
67
- t = t - INDEX;
68
- std::cout << distance (adj, cost, s, t);
67
+ }
68
+ int s, t;
69
+ std::cin >> s >> t;
70
+ s = s - INDEX;
71
+ t = t - INDEX;
72
+ std::cout << distance (adj, cost, s, t);
69
73
}
0 commit comments