Cod sursa(job #2098187)

Utilizator alexandra_paticaAndreea Alexandra Patica alexandra_patica Data 2 ianuarie 2018 15:32:22
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 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;
vector<int>G[30002], T[30002];
queue<int>q;

void bfs()
{
    q.push(X);
    dist[X]=0;
    while (!q.empty()){
        x=q.front();
        q.pop();
        for (i=0; i<G[x].size(); i++){
            if (x<G[x][i]){
                if (dist[G[x][i]]==-1){
                    dist[G[x][i]]=dist[x]+T[x][i];
                    q.push(G[x][i]);
                }
            }
            else{
                if (dist[G[x][i]]==-1){
                    dist[G[x][i]]=dist[x]-T[x][i];
                    q.push(G[x][i]);
                }
            }
        }
    }
}

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", &x, &y, &c);
        G[x].push_back(y);
        G[y].push_back(x);
        T[x].push_back(c);
        T[y].push_back(c);

    }
    memset(dist, -1, sizeof(dist));
//    for (i=1; i<=n; i++) printf("%d ", dist[i]);
//    printf("\n");
    bfs();
//     for (i=1; i<=n; i++) printf("%d ", dist[i]);
//    printf("\n");
    printf("%d", dist[Y]);
    return 0;
}