Cod sursa(job #1198809)

Utilizator andi12Draghici Andrei andi12 Data 17 iunie 2014 11:30:16
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <cstdio>

using namespace std;
int lst[30001];
int vf[200050];
int urm[200050];
int dist[200050];
int v[200050];
int c[200050];
int nr;
void ad(int x,int y,int d)
{
    nr++;
    vf[nr]=y;
    urm[nr]=lst[x];
    lst[x]=nr;
    if(x<y)
        dist[nr]=d;
    else
        dist[nr]=-d;
}
void bfs(int x,int y)
{
    int i,p=1,u=1,poz;
    c[1]=x;
    v[x]=1;
    while(p<=u && v[y]==0)
    {
        poz=lst[c[p]];
        p++;
        while(poz!=0)
        {
            if(v[vf[poz]]==0)
            {
                u++;
                c[u]=vf[poz];
                v[vf[poz]]=v[c[p-1]]+dist[poz];
            }
            poz=urm[poz];
        }
    }
}
int main()
{
    FILE *in,*out;
    in=fopen("sate.in","r");
    out=fopen("sate.out","w");
    int n,m,i,x,y,a,b,d;
    fscanf(in,"%d%d%d%d",&n,&m,&x,&y);
    for(i=1;i<=m;i++)
    {
        fscanf(in,"%d%d%d",&a,&b,&d);
        ad(a,b,d);
        ad(b,a,d);
    }
    bfs(x,y);
    fprintf(out,"%d",v[y]-1);
    return 0;
}