Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <stack>
- using namespace std;
- typedef pair<int,int> arc;
- int n,m;
- void dij2(vector< vector<arc> > &g, int x, int y, int max){
- vector<bool> vist(n,false);
- vector<int> dist(n,max);
- priority_queue<arc, vector<arc>, greater<arc> > q;
- vector<int> pares(n,-1);
- q.push(arc(0,x));
- dist[x]=0;
- while(not q.empty()){
- int act=q.top().second;
- q.pop();
- if(not vist[act]){
- vist[act]=true;
- for(int i=0;i<g[act].size();++i){
- int a=g[act][i].first;
- int b=g[act][i].second;
- if(dist[b]>dist[act]+a){
- dist[b]=dist[act]+a;
- q.push(arc(dist[act]+a,b));
- pares[b]=act;
- }
- }
- }
- }
- if(pares[y]==-1) cout << "no path from " << x << " to " << y << endl;
- else{
- stack<int> s;
- s.push(y);
- int c=pares[y];
- while(c!=x){
- s.push(c);
- c=pares[c];
- }
- s.push(x);
- while(s.size()>1){
- cout << s.top() << " ";
- s.pop();
- }
- cout << s.top() << endl;
- s.pop();
- }
- }
- int main(){
- int max=1;
- while(cin >> n){
- vector< vector<arc> > g(n);
- cin >> m;
- int u,v,c;
- for(int i=0;i<m;++i){
- cin >> u >> v >> c;
- max=max+c;
- g[u].push_back(arc(c,v));
- }
- int x,y;
- cin >> x >> y;
- dij2(g,x,y,max);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement