Advertisement
Israt_afroz

spoj

Mar 30th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1.    #include<bits/stdc++.h>
  2. using namespace std;
  3. const long inf = numeric_limits<long>::max();
  4. void shortest(vector<pair<int,pair<int,int> > >&v,int n,int m,int p)
  5. { vector<long>dist(n,inf);
  6.     vector<int>vp(n,0);
  7.     for(int i=0;i<n;i++)
  8.     {
  9.         for(int j=0;j<m;j++)
  10.         {  dist[p]=0;
  11.             int x=v[j].first;
  12.             int y=v[j].second.first;
  13.             int z=v[j].second.second;
  14.             if(dist[x]!=inf && dist[y]>(dist[x]+z))
  15.             {dist[y]=dist[x]+z;
  16.             if(i==n-1) vp[y]=1;}
  17.         }
  18.     }
  19.     for(int i=0;i<n;i++)
  20.    {
  21.         if(i==p) cout<<"0"<<endl;
  22.        else if(vp[i]==1) cout<<"-"<<endl;
  23.         else if(dist[i]==inf) cout<<"*"<<endl;
  24.        
  25.         else cout<<dist[i]<<endl;
  26.     }
  27. }
  28.  
  29. int main()
  30. {
  31.     int n,m,p;
  32.     cin>>n>>m;
  33.     vector<pair<int,pair<int,int> > >v;
  34.     for(int i=0;i<m;i++)
  35.     {
  36.         int x,y,z;
  37.         cin>>x>>y>>z;
  38.         x--;y--;
  39.         v.push_back(make_pair(x,make_pair(y,z)));
  40.     }
  41.     cin>>p; p--;
  42.     shortest(v,n,m,p);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement