Cod sursa(job #736189)

Utilizator Theorytheo .c Theory Data 18 aprilie 2012 01:55:03
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.04 kb
#include<fstream>
#include<stdlib.h>
#define nmax 300004
using namespace std;
ifstream fin("sate.in");
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 && dis[y]==0)
    {
        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;



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

        delete q;
    }
    return dis[y] ;


}
void read()
{
    int i,j , d;
    fin >> n >> m >> x >>y;
    for(i = 1; i <= n ;i++)
    {
        A[i] = (int *) realloc(A[i], sizeof(int));
        A[i][0] = 0;
        B[i] = (int *) realloc(B[i], sizeof(int));
        B[i][0] = 0;
         }
    for(k =1; k <= m ;k++)
    {
        fin >> i >> j >>d;
        A[i][0]++;
        A[i] = (int *) realloc(A[i] , (A[i][0] + 1 ) * sizeof(int));
        B[i] = (int *) realloc(B[i] , (A[i][0] + 1 ) * sizeof(int));
        A[j][0] ++;
        A[j] = (int *) realloc(A[j],  (A[j][0] + 1) * sizeof(int));
        B[j] = (int *) realloc(B[j],  (A[j][0] + 1) * sizeof(int));
        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()
{
    read();
    fin.close();
    return 0;
}