Cod sursa(job #972920)

Utilizator gbi250Gabriela Moldovan gbi250 Data 12 iulie 2013 21:48:45
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <cstdio>
#include <vector>
#include <queue>
#define pb push_back
#define SIZE 30001
using namespace std;
int n, m, x, y, i, a, b, d, nod, cost[SIZE];
struct graph
{
    int node, dist;
} c;
queue <int> q;
vector <graph> v[SIZE];
int main()
{
    freopen("sate.in", "r", stdin);
    freopen("sate.out", "w", stdout);
    scanf("%d %d %d %d", &n, &m, &x, &y);
    for(i=1;i<=m;++i)
    {
        scanf("%d %d %d", &a, &b, &d);
        c.node=b; c.dist=d;
        v[a].pb(c);
        c.node=a; c.dist=-d;
        v[b].pb(c);
    }
    for(i=1;i<=n;++i)
        cost[i]=-1;
    cost[x]=0;
    q.push(x);
    while(!q.empty())
    {
        nod=q.front(); q.pop();
        for(vector <graph> :: iterator it=v[nod].begin(); it!=v[nod].end(); ++it)
        {
            c=*it;
            if(cost[c.node]==-1)
            {
                q.push(c.node);
                cost[c.node]=c.dist+cost[nod];
                if(c.node==y)
                {
                    printf("%d\n", cost[y]);
                    return 0;
                }
            }
        }
    }
    printf("%d\n", cost[y]);
    return 0;
}