Cod sursa(job #297738)

Utilizator O_NealS. Alex O_Neal Data 5 aprilie 2009 16:04:34
Problema Sate Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include<stdio.h>


int n,x,y,xx,yy,s,d,p[3000],b[3000];
long dist[3000],aux,c,m;
long a[6000][500];

void BF(int k,int k2)
  {
      b[1]=k;
      p[k]=1;
      s=d=1;
      while(s<=d && !p[k2])
        {
            for(int i=1; i<=a[b[s]][0] && !p[k2] ; i+=2)
               if( !p[ a[b[s]][i]  ])
                 {
                     p[ a[b[s]][i] ]=1;
                     d++;
                     b[d]=a[b[s]][i];
                     if( a[b[s]][i] > b[s] ) dist[ a[b[s]][i] ] = dist[b[s]]+a[b[s]][i+1] ;
                           else dist[ a[b[s]][i]  ]=dist[b[s]]-a[b[s]][i+1] ;
                 }    
            s++;
       }
     printf("%ld",dist[k2]);
                 
  }
  
  
int main()
 {
     freopen("sate.in","r",stdin);
     freopen("sate.out","w",stdout);
     scanf("%d%ld%d%d",&n,&m,&x,&y);
//     
//     for(int i=1; i<=n; i++)
//        for(int j=1; j<=n; j++)
//            a[i][j]=32000;
//           
     for(int i=1; i<=m; i++)
        { scanf("%d%d%ld",&xx,&yy,&c); 
          
          a[xx][0]++; 
          a[xx][a[xx][0]]=yy; 
          a[xx][0]++;
          a[xx][a[xx][0]]=c;
          
          a[yy][0]++;
          a[yy][a[yy][0]]=xx;
          a[yy][0]++;
          a[yy][a[yy][0]]=c;
          
          }
        
     if(x<y) BF(x,y);
          else BF(y,x);
     
//     printf("\n\n");
//     for(int i=1; i<=n; i++)
//        printf("%ld ",b[i]);
//

//    printf("\n\n");
//    for(int i=1; i<=n; i++)
//        {for(int j=1; j<=n; j++)
//            printf("%6d ",a[i][j]);
//         printf("\n");         
//        }
//     
     //printf("%d",n);
     return 0;
 }