Cod sursa(job #1129148)

Utilizator pauldinudinu paul pauldinu Data 27 februarie 2014 20:25:50
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
const int N=30025;
int n,m,rep1,rep2,d[N];

struct pereche
{
    int vf, di;
};

vector <pereche> a[N];
queue <int> q;
void citire()
{
    int i,x,y,d1;
    pereche aux;
    f>>n>>m>>rep1>>rep2;
    for(i=1; i<=m; i++)
    {
        f>>x>>y>>d1;
        aux.vf = y;
        aux.di = d1;
        a[x].push_back(aux);
        aux.vf = x;
        a[y].push_back(aux);

    }
}
void bfs()
{
    int x,y,c,ok=1;
    q.push(rep1);
    while(!q.empty()&&ok!=0)
    {

        x=q.front();
        q.pop();
        for(int i=0; i<a[x].size(); i++)
        {
            y=a[x][i].vf;
            c=a[x][i].di;
            if(!d[y])
            {
              if (x < y) d[y] = d[x] + c;
            else d[y]=d[x]-c;

                q.push(y);
            }
            if(y==rep2)
            {
                ok=0;
                break;
            }
        }
    }
}
int main()
{
    citire();
    bfs();
    g<<d[rep2];
    return 0;
}