Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <queue>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define Element pair<int, int>
- void addEdge(vector<pair<int, int>>adj[], int u, int v, int wt)
- {
- adj[u].push_back(make_pair(v, wt));
- adj[v].push_back(make_pair(u, wt));
- }
- vector<int> Dijkstra(vector<pair<int, int>>adj[], int s, int V)
- {
- priority_queue<Element, vector<Element>, greater<Element>> pq;
- pq.push(make_pair(0, s));
- vector<int>dist(V, INF);
- dist[s] = 0;
- while (!pq.empty())
- {
- int v = pq.top().second;
- for (auto i : adj[v])
- {
- int u = (i).first;
- int weight = i.second;
- if (dist[u] > dist[v] + weight)
- {
- dist[u] = dist[v] + weight;
- pq.push(make_pair(dist[u], u));
- }
- }
- pq.pop();
- }
- return dist;
- }
- int main() {
- int n, m;
- cin>>n>>m;
- vector<pair<int, int>>gr[n];
- for(int i=0;i<m;i++)
- {
- int u, v, w;
- cin>>u>>v>>w;
- addEdge(gr, u-1, v-1, w);
- }
- int s;
- cin>>s;
- vector<int>res=Dijkstra(gr, s-1, n);
- for(auto el: res)
- {
- if(el!=0)
- {
- cout<<el<<" ";
- }
- }
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement