Pagini recente » Cod sursa (job #616983) | Cod sursa (job #121217) | Cod sursa (job #180426) | Cod sursa (job #1604755) | Cod sursa (job #2033508)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
//#define INF 0x3f3f3f3f
using namespace std;
ifstream in ("sate.in");
ofstream out ("sate.out");
vector<pair<int,int> >graf[30001];
int dist[30001];
queue<int> q;
int n,m,x,Y;
bool viz[30001];
void bfs()
{
while(!q.empty())
{
int a=q.front();
q.pop();
for(int y=0;y<graf[a].size();y++)
{
if(!viz[graf[a][y].first])//dist[graf[a][y].first]==INF)
{
viz[graf[a][y].first]=1;
if(graf[a][y].first<a)
dist[graf[a][y].first]=dist[a]-graf[a][y].second;
else if(graf[a][y].first>a)
dist[graf[a][y].first]=dist[a]+graf[a][y].second;
//if (graf[a][y].first==Y) return;
q.push(graf[a][y].first);
}
}
}
}
int abs(int k)
{
if (k>=0) return k;
return k*(-1);
}
int main()
{
int i,a,b,c;
in>>n>>m>>x>>Y;
for(i=1;i<=n;i++)
{
in>>a>>b>>c;
graf[a].push_back(make_pair(b,c));
graf[b].push_back(make_pair(a,c));
}
//for(i=1;i<=n;i++)
// dist[i]=INF;
//dist[x]=0;
q.push(x);
bfs();
out<<abs(dist[Y]);
}