Cod sursa(job #68098)

Utilizator ZeusCatalin Tiseanu Zeus Data 26 iunie 2007 14:11:52
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.09 kb

using namespace std;

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

vector<int> g[30100], co[30100];
int N, M, D[30100], X, Y, q[30100], u[30100];

char * p;

int baga()
{
    int ret = 0;
    while( *p != '\n' && *p != '\0' && isdigit(*p) )
          ret *= 10,
          ret += *p - '0',
          p++;
    return ret; 
}

int main()
{
    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);
    
    char buf[100];
    
    scanf("%d %d %d %d \n", &N, &M, &X, &Y);
    
    int x, y, c, l, r, el;
    
    for( int i = 1; i <= N; i++ )
         g[i].clear(), co[i].clear();
    
    for( int i = 0; i < M; i++ )
    {
/*
         scanf("%d %d %d", &x, &y, &c);
*/
         gets( buf );
                  
         p = buf;
         x = baga(), p++, y = baga(), p++, c = baga();
         
         g[x].push_back(y), g[y].push_back(x);
         co[x].push_back(c), co[y].push_back(c);
    }
    
    for( int i = 1; i <= N; i++ )
         D[i] = (1<<28),
         u[i] = 0;
         
    D[X] = 0;
    
    for( q[ l = r = 0 ] = X; l <= r; l++ )
    {
         el = q[l];
         u[el] = 1;
         
         for( int j = 0; j < (int)g[el].size(); ++j )
         {
              y = g[el][j], c = co[el][j];
              
              if( u[y] )
                  continue;
              
              u[y] = 1;
              q[++r] = y;
              
              if( X < el )
              {
                  if( y < X )
                      D[y] = c - D[el];
                  else if( y < el )
                      D[y] = D[el] - c;
                  else
                      D[y] = D[el] + c;     
              }
              else
              {
                  if( y < el )
                      D[y] = D[el] + c;
                  else if( y < X )
                      D[y] = D[el] - c;
                  else
                      D[y] = c - D[el];     
              }
                   
         }
    }
    
    printf("%d\n", D[Y] );
    
    return 0;    
}