Cod sursa(job #1781204)

Utilizator KOzarmOvidiu Badea KOzarm Data 16 octombrie 2016 18:59:12
Problema Sate Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
#include <vector>
#include <deque>

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

struct drum
{
    int x;
    long long cst;
}dr;

vector <drum> G[30005];
deque <drum> d;

bool viz[30005];
int n,m,x,y;

long long bfs(int poz)
{
    dr.x=poz;
    dr.cst=0;
    viz[poz]=1;
    d.push_back(dr);
    while(d.size())
    {
        dr=d.front();
        d.pop_front();
        poz=dr.x;
        int cost=dr.cst;
        for(int i=0;i<G[poz].size();i++)
        if(G[poz][i].x==y)
        {
            if(y<poz)
                return cost-G[poz][i].cst;
            else
                return cost+G[poz][i].cst;
        }
        else
        if(viz[G[poz][i].x]==0)
        {
            if(G[poz][i].x>poz)
                dr.cst=G[poz][i].cst+cost;
            else
                dr.cst=cost-G[poz][i].cst;
            dr.x=G[poz][i].x;
            d.push_back(dr);
            viz[G[poz][i].x]=1;
        }
    }
    return 0;
}

int main()
{
    fin>>n>>m>>x>>y;
    if(x>y)
        swap(x,y);
    for(int i=1;i<=n;i++)
    {
        int a,b,d;
        fin>>a>>b>>d;
        dr.x=a;
        dr.cst=d;
        G[b].push_back(dr);
        dr.x=b;
        G[a].push_back(dr);
    }
    fout<<bfs(x);
    return 0;
}