Advertisement
master_roby3

Untitled

Mar 3rd, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXL 60000
  5.  
  6. int N, M, S, H;
  7.  
  8. typedef struct nodo{
  9.   int n;
  10.   int L;
  11. }nodo;
  12.  
  13. int main() {
  14.   freopen("input.txt","r",stdin);
  15.   freopen("output.txt","w",stdout);
  16.  
  17.   scanf("%d %d %d %d", &N, &M, &H, &S);
  18.   int i;
  19.   nodo nodi[50000];
  20.   int A, B, L;
  21.   for(i=0; i<M; i++){
  22.     scanf("%d %d %d", &A, &B, &L);
  23.     if(nodi[A-1].L==0) nodi[A-1].L=MAXL;
  24.     if(nodi[B-1].L==0) nodi[B-1].L=MAXL;
  25.     if(nodi[A-1].L>L){
  26.       nodi[A-1].n=B;
  27.       nodi[A-1].L=L;
  28.     }
  29.     if(nodi[B-1].L>L){
  30.       nodi[B-1].n=A;
  31.       nodi[B-1].L=L;
  32.     }
  33.   }
  34.  
  35.   int numg=0;
  36.   int current=H;
  37.   int visited[50000];
  38.   while(current!=S && visited[current-1]!=1){
  39.     visited[current-1]=1;
  40.     current=nodi[current-1].n;
  41.     numg++;
  42.   }
  43.  
  44.   if(current==S) printf("%d", numg);
  45.   else printf("%d", -1);
  46.  
  47.   return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement