Cod sursa(job #901060)

Utilizator mads2194FMI - Andrei Stroe mads2194 Data 28 februarie 2013 23:56:50
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 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];

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

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

void solve()
{
    int x,y;
    d[st]=1;
    queue <int> q;
    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();

    solve();
    printf("%d",d[end]-1);

    return 0;
}