Cod sursa(job #1848927)

Utilizator GoogalAbabei Daniel Googal Data 16 ianuarie 2017 20:43:27
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <fstream>
#include <vector>
#include <queue>
#define nmax 30001

using namespace std;

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

int n,m,viz[nmax],x,y;
long long cost[nmax];

vector < pair < int, long long > > leg[nmax];
queue < int > coada;

inline void read()
{
    int i,a,b,z;
    fin>>n>>m>>x>>y;
    if(x>y)
        swap(x,y);
    for(i=1; i<=m; i++)
    {
        fin>>a>>b>>z;
        leg[a].push_back(make_pair(b,z));
        leg[b].push_back(make_pair(a,-1*z));
    }
    fin.close();
}

void BFS()
{
    int i;
    viz[x]=true;
    coada.push(x);
    while(!coada.empty())
    {
        x=coada.front();
        coada.pop();
        if(x==y)
            return;
        for(i=0; i<leg[x].size(); i++)
        {
            if(!viz[leg[x][i].first])
            {
                viz[leg[x][i].first]=true;
                cost[leg[x][i].first]=cost[x]+leg[x][i].second;
                coada.push(leg[x][i].first);
            }
            else if(cost[leg[x][i].first]>cost[x]+leg[x][i].second)
            {
                viz[leg[x][i].first]=true;
                cost[leg[x][i].first]=cost[x]+leg[x][i].second;
                coada.push(leg[x][i].first);
            }
        }
    }
}

int main()
{
    read();
    BFS();
    fout<<cost[y];
    fout.close();
    return 0;
}