Cod sursa(job #1160370)

Utilizator CostinVFMI CostinVictorGabriel CostinV Data 30 martie 2014 15:10:39
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include<fstream>
#include<vector>
#include<queue>

using namespace std;

struct nod
{
    int x, y;
};

nod make_nod(int x, int y)
{
    nod p;
    p.x=x;
    p.y=y;

    return p;
}

void bfs (vector <nod> *g, int x, int y, int *dis, bool *viz)
{
    ofstream o("sate.out");
    queue <int> q;
    int curent, next;
    dis[x]=0;

    q.push(x);

    while(!q.empty())
    {
        curent = q.front();
        viz[curent] = 1;
        q.pop();

        for(int i=0; i<g[curent].size(); i++)
        {
            if(!viz[g[curent][i].x])
            {
                next = g[curent][i].x;

                if(next>curent)
                    dis[next] = dis[curent] + g[curent][i].y;
                else
                    dis[next] = dis[curent] - g[curent][i].y;

                if(next == y)
                {
                    o<<dis[next];
                    return;
                }

                q.push(next);
            }
        }
    }
}

int main()
{
    int n, m, x, y;
    int a, b, c, *dis;
    bool *viz;
    ifstream f("sate.in");

    f>>n>>m>>x>>y;

    vector <nod> *g;
    g = new vector<nod> [n+1];
    viz = new bool [n+1];
    dis = new int [n+1];

    for(int i=0; i<m; i++)
    {
        f>>a>>b>>c;
        nod p;
        g[a].push_back(make_nod(b, c));
        g[b].push_back(make_nod(a, c));
    }

    bfs (g, x, y, dis, viz);

    return 0;
}