Cod sursa(job #1888470)

Utilizator FredyLup Lucia Fredy Data 22 februarie 2017 09:36:35
Problema Sate Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <fstream>
#include <vector>
#include <string.h>
#include <queue>

using namespace std;

ifstream fin("sate.in");
ofstream fout("sate.out");

#define lim 30010
#define maxb 200000
int n,m,x,y,a,b,c;
int dist[lim];
bool viz[lim];
struct loc{int sat,d;};
vector <loc> G[lim];
char buff[maxb];
int dr=0;

int getint()
{
    int nr=0;
    fin.read(buff,maxb);
    while(!(buff[dr]>='0' && buff[dr]<='9'))
    {
        if(++dr >= maxb)
        {
            fin.read(buff,maxb);
            dr=0;
        }

    }
    while(buff[dr]>='0' && buff[dr]<='9')
    {
        nr=nr*10+buff[dr]-'0';
        if(++dr >= maxb)
        {
            fin.read(buff,maxb);
            dr=0;
        }
    }
    return nr;
}



void bfs(int nod)
{
    queue <int> q;
    q.push(nod);
    dist[nod]=0;

    while(!q.empty())
    {
        nod=q.front();
        q.pop();
        for(auto it:G[nod])
        {
            if(!viz[it.sat])
            {
                viz[it.sat]=true;
                if(it.sat>nod)
                {
                    dist[it.sat]=dist[nod]+it.d;
                    q.push(it.sat);
                }
                if(it.sat<nod)
                {
                    dist[it.sat]=dist[nod]-it.d;
                    q.push(it.sat);
                }

                if(it.sat==y)
                {
                    fout<<dist[y];
                    fout.close();
                    return;
                }
            }
        }
    }

}


int main()
{
    n=getint();
    m=getint();
    x=getint();
    y=getint();
    for(int i=1; i<=m; i++)
    {
        a=getint();
        b=getint();
        c=getint();
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }

    bfs(x);

    fin.close();
    fout.close();
    return 0;
}