Pagini recente » Cod sursa (job #1079414) | Cod sursa (job #2975705) | Cod sursa (job #2640580) | Cod sursa (job #744329) | Cod sursa (job #369468)
Cod sursa(job #369468)
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
#define nmax 30005
typedef pair <int, int> ii;
int n, m, x, y, d [nmax];
vector <ii> g [nmax];
queue <int> q;
void scan ()
{
int i, a, b, c;
scanf ("%d%d%d%d", &n, &m, &x, &y);
for (i=1; i <= m; ++i)
{
scanf ("%d%d%d", &a, &b, &c);
g [a].push_back (ii (b, c));
g [b].push_back (ii (a, c));
}
}
void bfs ()
{
vector <ii> :: iterator it;
int nod, v;
q.push (x);
d [x]=1;
while (!q.empty ())
{
nod=q.front ();
v=d [nod];
q.pop ();
for (it=g [nod].begin (); it != g [nod].end (); ++it)
if (d [it->first] == 0)
{
if (it->first < nod)
d [it->first] = d [nod] - it->second;
else
d [it->first] = d [nod] + it->second;
if (it->first == y)
return ;
q.push (it->first);
}
}
}
int main ()
{
freopen ("sate.in", "r", stdin);
freopen ("sate.out", "w", stdout);
scan ();
bfs ();
printf ("%d\n", d [y]-1);
return 0;
}