Cod sursa(job #1706539)

Utilizator Alin23Alin Dinu Alin23 Data 22 mai 2016 19:07:28
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <iostream>
#include <stdio.h>
#include <fstream>

using namespace std;

ifstream in("sate.in");
ofstream out("sate.out");

typedef struct
 {
   int v;
   int c;
 } lista;

lista p[30005][100];
int el[30005]={0};
int v[30005]={0};
int d[30005]={0};
int coada[30005<<1];
int n, m, x, y;

void buildgraf (void)
{
    int i,x0,y0,d;
    in >> n >> m >> x >> y;
    for (i=1; i<=m; i++)
    {
        in >> x0 >> y0 >> d;
        el[x0]++;
        el[y0]++;
        p[x0][el[x0]].v=y0;
        p[x0][el[x0]].c=d;
        p[y0][el[y0]].v=x0;
        p[y0][el[y0]].c=-d;
    }
}

void bf ()
{
    int i, li=1, lf=1, vf;
    v[x]=1;
    coada[lf++]=x;
    while (li!=lf)
    {
        vf=coada[(lf++)%30005];
        v[vf]=1;
        for (i=1; i<=el[vf]; i++)
        if (v[p[vf][i].v]==0)
        {
            coada[(lf++)%30005]=p[vf][i].v;
            v[p[vf][i].v]=1;
            d[p[vf][i].v]=d[vf]+p[vf][i].c;
        }
    }
    out << d[y];
}

int main()
{
    buildgraf();
    bf();
    in.close();
    out.close();
    return 0;
}