Cod sursa(job #2098207)

Utilizator alexandra_paticaAndreea Alexandra Patica alexandra_patica Data 2 ianuarie 2018 15:53:23
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

struct punct
{
    int nod, c;
};
int n, m, i,x, y, dist[30002], X, Y, c, u;
vector<punct>G[30002], T[30002];
queue<int>q;

void bfs()
{
    q.push(X);
    dist[X]=0;
    while (!q.empty()){
        x=q.front();
        q.pop();
        u=G[x].size();
        for (i=0; i<u; i++){
                if (dist[G[x][i].nod]==999999999){
                    dist[G[x][i].nod]=dist[x]+G[x][i].c;
                    q.push(G[x][i].nod);
                }
        }
    }
}

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<=n; i++) dist[i]=999999999;
    for (i=1; i<=m; i++){
        scanf("%d%d%d", &x, &y, &c);
        G[x].push_back({y, c});
        G[y].push_back({x, -1*c});
    }
    bfs();
    printf("%d", dist[Y]);
    return 0;
}