Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXL 60000
- int N, M, S, H;
- typedef struct nodo{
- int n;
- int L;
- }nodo;
- int main() {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- scanf("%d %d %d %d", &N, &M, &H, &S);
- int i;
- nodo nodi[50000];
- int A, B, L;
- for(i=0; i<M; i++){
- scanf("%d %d %d", &A, &B, &L);
- if(nodi[A-1].L==0) nodi[A-1].L=MAXL;
- if(nodi[B-1].L==0) nodi[B-1].L=MAXL;
- if(nodi[A-1].L>L){
- nodi[A-1].n=B;
- nodi[A-1].L=L;
- }
- if(nodi[B-1].L>L){
- nodi[B-1].n=A;
- nodi[B-1].L=L;
- }
- }
- int numg=0;
- int current=H;
- int visited[50000];
- while(current!=S && visited[current-1]!=1){
- visited[current-1]=1;
- current=nodi[current-1].n;
- numg++;
- }
- if(current==S) printf("%d", numg);
- else printf("%d", -1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement