Advertisement
add1ctus

Автопат

Mar 2nd, 2015
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <cstring>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int t2;
  11. long long dalecini[10001]={-1};
  12. bool poseteno[10001]={false};
  13. vector<vector<pair<int,int> > > povrzani(10001);
  14.  
  15. void dfs(int teme)
  16. {
  17.     if(poseteno[t2])
  18.         return;
  19.     for(int i=0;i<povrzani[teme].size();i++)
  20.     {
  21.         if(!poseteno[povrzani[teme][i].first])
  22.         {
  23.             if(povrzani[teme][i].first>teme)
  24.                 dalecini[povrzani[teme][i].first]=povrzani[teme][i].second+dalecini[teme];
  25.             else
  26.                 dalecini[povrzani[teme][i].first]=dalecini[teme]-povrzani[teme][i].second;
  27.             poseteno[povrzani[teme][i].first]=true;
  28.             dfs(povrzani[teme][i].first);
  29.         }
  30.     }
  31. }
  32.  
  33. int main()
  34. {
  35.     int t1,k,n;
  36.     cin>>t1>>t2>>k>>n;
  37.     if(t1>t2)
  38.         swap(t1,t2);
  39.     memset(dalecini,-1,sizeof(long long)*10001);
  40.     for(int i=0;i<n;i++)
  41.     {
  42.         int p,k,d;
  43.         cin>>p>>k>>d;
  44.         povrzani[p].push_back(make_pair(k,d));
  45.         povrzani[k].push_back(make_pair(p,d));
  46.     }
  47.     dalecini[t1]=0;
  48.     dfs(t1);
  49.     if(poseteno[t2])
  50.         cout<<dalecini[t2];
  51.     else
  52.         cout<<-1;
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement