Pagini recente » Cod sursa (job #809757) | Cod sursa (job #1559820) | Cod sursa (job #48104) | Cod sursa (job #619619) | Cod sursa (job #1209470)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int MAXN = 30011;
int N, M, X, Y, dist[MAXN], viz[MAXN], lung[MAXN];
vector < pair <int, int> > G[MAXN];
vector < pair <int, int> > :: iterator it;
queue <int> q;
void citire()
{
fin >> N >> M >> X >> Y;
for (int i = 0; i < M; ++i)
{
int x, y, cost;
fin >> x >> y >> cost;
G[x].push_back(make_pair(y, cost));
G[y].push_back(make_pair(x, cost));
}
for (int i = 1; i <= N; ++i)
lung[i] = G[i].size();
}
void BFS()
{
++viz[X];
q.push(X);
while ( !q.empty() )
{
int tata = q.front();
q.pop();
for (it = G[tata].begin(); it != G[tata].end(); ++it)
{
int fiu = it->first;
int cost = it->second;
if ( !viz[fiu] )
{
++viz[fiu];
q.push(fiu);
if ( tata < fiu )
dist[fiu] = dist[tata] + cost;
else
dist[fiu] = dist[tata] - cost;
}
}
}
}
int main ()
{
citire();
BFS();
fout << dist[Y];
fin.close();
fout.close();
return 0;
}