Cod sursa(job #736211)

Utilizator Theorytheo .c Theory Data 18 aprilie 2012 02:54:07
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.25 kb
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#define nmax 30004
using namespace std;

ofstream fout("sate.out");
int i, j ,n , m , x, y, k ;
bool viz[nmax];
int *A[nmax] , *B[nmax];
struct nod{int inf; nod *urm;} ;
nod *p , *u;

int dis[nmax];
int bfs(int x,int y)
{
    int i, j;

    nod *q;
    viz[x] = true;
    q = new nod;
    q -> inf = x;
    q -> urm = NULL;
    p = u = q;
    while(p != NULL )
    {
        x = p -> inf;
        //fout << p -> inf <<" " ;
        for(i = 1; i <= A[x][0] ; i ++)
        {
            if(viz[A[x][i]] == false  )
            {
                viz[A[x][i]] = true;
                //fout << B[x][i ] <<" " ;
                if(x < A[x][i])
                dis[A[x][i]] = dis[x] + B[x][i ]    ;
                    else
                  dis[A[x][i]] = dis[x] - B[x][i ];
                q = new nod;
                q -> inf = A[x][i] ;
                q -> urm = NULL;
                u -> urm = q;
                u = q;
                if(dis[y])
                return dis[y];




            }
        }
        q = p;
        //fout << p -> inf<<" ";
        p = p->urm;

        delete q;
    }
    return dis[y] ;


}
void read()
{
    int i,j , d;
    scanf("%d %d %d %d", &n ,&m , &x, &y);

   for(i = 1; i <=n ;i++)
   {
       A[i] = (int *) malloc(sizeof(int) );
        B[i] = (int *) malloc( sizeof(int) );
        A[i][0] = B[i][0] = 0;
   }

    for(k = 1; k <= m ; k++)
    {
        scanf("%d %d %d",&i, &j, &d);

        A[i][0] ++;
        A[j][0] ++;
    }
    for(i = 1; i <= n ;i++)
    {
         A[i] = (int *) malloc((A[i][0] + 1) *sizeof(int));
         B[i] = (int *) malloc((A[i][0] + 1 ) *sizeof(int));
         A[i][0] = B[i][0] = 0;
    }


        fseek(stdin, 0, SEEK_SET);
    scanf("%d %d %d %d", &n ,&m , &x, &y);
    for(k =1; k <= m ;k++)
    {
        scanf("%d %d %d",&i, &j, &d);
        A[i][0]++;

        A[j][0]++;

        B[i][A[i][0]] = d;
        B[j][A[j][0]] = d;
        A[i][A[i][0]] = j;
        A[j][A[j][0]] = i;

    }
     fout << bfs(x,y);


   // fout<< dis[i] <<" " ;

}
int main()
{
    freopen("sate.in", "r", stdin);
    read();
    fclose(stdin);
    return 0;
}