Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include <queue>
- #define INF 32000
- using namespace std;
- vector < vector< pair<long long, long long> > > graph;
- long long n, m, x, y, z;
- void dijkstra(int s){
- vector<long long> d(n+1, INF);
- d[s]=0;
- priority_queue <pair<int, int> > q;
- q.push(make_pair(s, 0));
- while(!q.empty()){
- pair<int, int> top = q.top();
- q.pop();
- for(int i=0; i<graph[top.first].size(); i++){
- int cur_vert=graph[top.first][i].first;
- int cur_dist = graph[top.first][i].second;
- if(d[cur_vert]>d[top.first]+cur_dist){
- d[cur_vert]=d[top.first]+cur_dist;
- q.push(make_pair(cur_vert, d[top.first]+cur_dist));
- }
- }
- }
- if(d[n]==INF) cout<<-1<<endl;
- else cout<<d[n]<<endl;
- }
- int main()
- {
- cin>>n>>m;
- graph.resize(n+1);
- for(int i=0; i<m; i++){
- cin>>x>>y>>z;
- graph[x].push_back(make_pair(y, z));
- }
- dijkstra(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement