structEdge{ int to; int last; int dis; }edge[500001];
int edge_number; int end[500001];
voidadd_edge(int from , int to , int dis){ edge[++ edge_number].to = to; edge[edge_number].dis = dis; edge[edge_number].last = end[from]; end[from] = edge_number; }
int dis[500001]; int n; bool vis[500001];
inlinevoidDijkstra(int s){ priority_queue< pair<int , int> , vector<pair<int , int> > , greater<pair <int , int> > > Q; for(int i = 1; i <= n; i ++){ if (i != s){ dis[i] = 0x3f3f3f3f; } } for (int i = 1; i <= n; ++i) Q.push(make_pair(dis[i], i)); while(!Q.empty()){ int v = Q.top().second; Q.pop(); if(vis[v] == true){ continue; } vis[v] = true; for(int i = end[v]; i != 0;i = edge[i].last){ int to = edge[i].to; if(dis[to] > dis[v] + edge[i].dis){ dis[to] = dis[v] + edge[i].dis; Q.push(make_pair(dis[to],to)); } } } }
intmain(){ int m , s; int a , b , c; cin >> n >> m >> s; for(int i = 1; i <= m; i ++){ cin >> a >> b >> c; add_edge(a , b , c); } Dijkstra(s); for(int i = 1; i <= n; i ++){ cout << dis[i] << " "; } return0; } //Written By Payphone-X
structEdge{ int to; int dis; int last; }edge[500001];
int end[500001]; int edge_number;
voidadd_edge(int from , int to , int dis){ edge[++edge_number].to = to; edge[edge_number].dis = dis; edge[edge_number].last = end[from]; end[from] = edge_number; }
queue<int > Q; bool visited[500001]; int dis[500001]; int n , m , s; int a , b , c;
voidSPFA(int s){ int cnt = s , sum = 0; for(int i = 1; i <= s; i ++){ dis[i] = 0x3f3f3f3f; } dis[s] = 0; Q.push(s); visited[s] = true; int first; while(!Q.empty()){ first = Q.front(); while(dis[first]*cnt > sum){ Q.pop(); Q.push(first); first = Q.front(); } Q.pop(); cnt--; sum = sum - dis[first]; visited[first] = false; for(int i = end[first]; i != 0; i = edge[i].last){ if(dis[edge[i].to] > dis[first] + edge[i].dis){ dis[edge[i].to] = dis[first] + edge[i].dis; if(visited[edge[i].to] == false){ sum = sum + dis[edge[i].to]; cnt ++; Q.push(edge[i].to); } } } } }
intmain(){ cin >> n >> m >> s; for(int i = 1; i <= m; i ++){ cin >> a >> b >> c; add_edge(a , b , c); } SPFA(s); for(int i = 1; i <= n; i ++){ if(dis[i] == 0x3f3f3f){ cout << "2147483647" << " "; } else{ cout << dis[i] << " "; } } return0; } //Written By Payphone-X
structEdge{ int to; int last; int dis; }edge[500001];
int end[500001]; int edge_number;
voidadd_edge(int from , int to , int dis){ edge[++edge_number].to = to; edge[edge_number].dis = dis; edge[edge_number].last = end[from]; end[from] = edge_number; }
int visited[500001]; int dis[500001]; int n , m , s; int a , b , c;
inlinevoidSPFA(int s){ deque<int >Q; int cnt = s , sum = 0; for(int i = 1; i <= n; i ++){ dis[i] = 0x3f3f3f; } dis[s] = 0; Q.push_back(s); visited[s] = true; int first; while(!Q.empty()){ first = Q.front(); while(dis[first]*cnt > sum){ Q.pop_back(); Q.push_back(first); first = Q.front(); } Q.pop_front(); cnt--; sum-= dis[first]; visited[first] = false; for(int i = end[first]; i != 0 ; i = edge[i].last){ if(dis[edge[i].to] > dis[first] + edge[i].dis){ dis[edge[i].to] = dis[first] + edge[i].dis; if(visited[edge[i].to] == false){ sum+=dis[edge[i].to]; cnt ++; if(dis[edge[i].to] < dis[Q.front()]){ Q.push_front(edge[i].to); } else Q.push_back(edge[i].to); } } } } }
intmain(){ cin >> n >> m >> s; for(int i = 1; i <= m; i ++){ cin >> a >> b >> c; add_edge(a , b , c); } SPFA(s); for(int i = 1; i <= n; i ++){ if(dis[i] == 0x3f3f3f){ cout << "2147483647" << " "; } else{ cout << dis[i] << " "; } } return0; } //Written By Payphone-X