Pagini recente » Cod sursa (job #2621135) | Cod sursa (job #317467) | Cod sursa (job #2761469) | Cod sursa (job #742834) | Cod sursa (job #1484454)
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
ifstream fin ("sate.in");
ofstream fout ("sate.out");
struct muchie{
int nod, cost;
};
vector <muchie> L[100029];
int n,m,x1,x2,lera_lynn;
int cost[30009];
bool viz[30009];
void Citire()
{
int i,j,x,y,costt;
muchie w;
if (x1>x2) swap(x1,x2);
fin >> n >> m >> x1 >> x2;
for (i=1; i<=m; i++)
{
fin >> x >> y >> costt;
if (x>y) swap(x,y);
// daca ma duc din x in y e cost pozitiv
// daca ma duc din y in x e cost negativ
w.nod=y;
w.cost=costt;
L[x].push_back(w);
w.nod=x;
w.cost=costt * (-1);
L[y].push_back(w);
}
}
int Rezolva()
{
queue <int> q;
int i,j,k,costt,distanta;
q.push(x1);
viz[x1]=1;
while (!q.empty())
{
k=q.front();
q.pop();
for (j=0; j<L[k].size(); j++)
{
i = L[k][j].nod; costt=L[k][j].cost;
if (!viz[i])
{
cost[i]=cost[k]+costt;
viz[i]=1;
if (i==x2)
{
distanta=cost[i];
return distanta;
}
q.push(i);
}
}
}
}
void Afisare()
{
fout << lera_lynn << "\n";
}
int main ()
{
Citire();
lera_lynn = Rezolva();
Afisare();
fin.close();
fout.close();
return 0;
}