Luca25

Untitled

Nov 14th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("input.txt");
  6. ofstream fout("output.txt");
  7.  
  8. int n, m, j, k, a, b, c;
  9. vector<pair<int, int> > g[1000000];
  10. int v[10000000];
  11.  
  12. int dfs(int h, int u){
  13.     int l=500000000, z=0;
  14.     for(int x=0; x<g[h].size(); x++){
  15.         if(g[h][x].second<l){
  16.             l=g[h][x].second;
  17.             z=g[h][x].first;
  18.         }
  19.     }
  20.     if(z==k) return u+1;
  21.     if(v[z]==0){
  22.         v[z]=1;
  23.         dfs(z, u+1);
  24.     }else{
  25.         return -1;
  26.     }
  27. }
  28.  
  29. int main(){
  30.     fin >> n >> m >> j >> k;
  31.     for(int x=0; x<m; x++){
  32.         fin >> a >> b >> c;
  33.         g[a].push_back({b, c});
  34.         g[b].push_back({a, c});
  35.     }
  36.     fout << dfs(j, 0);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment