Cod sursa(job #1022472)

Utilizator simplicityFlorescu Emanuel Robert simplicity Data 5 noiembrie 2013 15:08:24
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n,m,x,y,i,dist;
struct nod{int s; int d;
};
vector <nod> v[30002];
int length[30002];
bool viz[30002];
using namespace std;
void citire()
{   int s1,s2;
    f>>n>>m>>x>>y;
    for(i=1;i<=m;i++)
    {
        f>>s1>>s2>>dist;
        nod na;
        na.s=s2;na.d=dist;
        v[s1].push_back(na);
        na.s=s1;na.d=dist;
        v[s2].push_back(na);

    }
}
void bfs(int x)
{
    viz[x]=true;
    queue <int> q;
    q.push(x);
    while(!q.empty())
    {
        int fr=q.front();
        q.pop();
        for(i=0;i<v[fr].size();i++)
        {

            nod nawd=v[fr][i];
            if(!viz[nawd.s])
            {
                q.push(nawd.s);
                viz[nawd.s]=true;
                if(fr>nawd.s)
                {
                    length[nawd.s]=length[fr]-nawd.d;
                }
                else
                {
                    length[nawd.s]=length[fr]+nawd.d;
                }
            }
        }
    }
}
int main()
{   citire();
    bfs(x);
    g<<length[y];
    return 0;
}