Pagini recente » Cod sursa (job #2048560) | Cod sursa (job #618168) | Cod sursa (job #2427919) | Cod sursa (job #3167958) | Cod sursa (job #2638825)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define Nmax 30005
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int vf, muchii, start, finish;
vector < vector < int > > Numere[Nmax];
queue < int > Coada;
int Distanta[Nmax];
void BFS()
{
int Nod, Vecin;
while( !Coada.empty() )
{
Nod = Coada.front();
Coada.pop();
for(unsigned int i = 0; i < Numere[Nod].size(); i ++)
{
Vecin = Numere[Nod][i].front();
if(Distanta[Vecin] == 0)
{
Distanta[Vecin] = Distanta[Nod] + Numere[Nod][i].back();
Coada.push(Vecin);
}
}
}
}
void Read()
{
fin >> vf >> muchii >> start >> finish;
for(int i = 1; i <= muchii; i ++)
{
int x, y, valoare;
fin >> x >> y >> valoare;
if(x < y)
{
Numere[x].push_back({y, valoare});
Numere[y].push_back({x, -valoare});
}
else
{
Numere[x].push_back({y, -valoare});
Numere[y].push_back({x, valoare});
}
}
for(int i = 1; i <= vf; i ++)
Distanta[i] = 0;
Coada.push(start);
BFS();
fout << Distanta[finish];
}
int main()
{
Read();
return 0;
}