Cod sursa(job #898804)

Utilizator mads2194FMI - Andrei Stroe mads2194 Data 28 februarie 2013 11:48:08
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include<cstdio>
#include<vector>
#include<queue>

#define NM 30002

using namespace std;

struct elem
{
    int d,to;
};

vector <elem> a[NM];

int n,st,end;
int d[NM];

elem me(int fd,int fto)
{
    elem e;
    e.d = fd;
    e.to = fto;
    return e;
}

void read()
{
    int m;
    int x,y,dr;
    scanf("%d %d %d %d",&n,&m,&st,&end);

    while(m--)
        {
            scanf("%d %d %d",&x,&y,&dr);
            a[x].push_back(me(dr,y));
            a[y].push_back(me(-dr,x));
        }
}

void solve()
{
    queue <int> q;
    int x,y;
    d[st]=0;
    q.push(st);

    while(!q.empty())
    {
        x=q.front();
        q.pop();

        for(size_t i=0;i<a[x].size();++i)
            {
                y=a[x][i].to;
                if(!d[y])
                    {
                        d[y]=d[x]+a[x][i].d;
                        if(d[end]) return;
                        q.push(y);
                    }
            }
    }
}

int main()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);

    read();

    //for(int i=1;i<=n;++i)
    //    if(! viz[i])
    solve();
    printf("%d",d[end]);

    return 0;
}