Pagini recente » Cod sursa (job #2219057) | Cod sursa (job #3218703) | Cod sursa (job #1817793) | Cod sursa (job #2939084) | Cod sursa (job #644239)
Cod sursa(job #644239)
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#define NMAX 30002
using namespace std;
typedef vector< pair<int, int> > lista;
vector<lista> a(NMAX);
//queue< pair<int, int> > c;
vector< pair<int, int> > c(NMAX);
int viz[NMAX], dist[NMAX];
int N, M, X, Y;
int main()
{
pair<int, int>curent;
int i, x, y, d, ok, yt;
int front, end;
lista::iterator it;
freopen("sate.in", "r", stdin);
freopen("sate.out", "w", stdout);
scanf("%d %d %d %d", &N, &M, &X, &Y);
for(i=0;i<M;++i)
{
scanf("%d %d %d", &x, &y, &d);
a[x].push_back(make_pair(y, d));
a[y].push_back(make_pair(x, d));
}
c[0] = make_pair(X, 0);
viz[curent.first] = 1;
front = end = 0;
while(front <= end)
{
curent = c[front++];
//viz[curent.first] = 1;
if(curent.first == Y)
{
break;
}
ok = 0;
for(it=a[curent.first].begin();it!=a[curent.first].end();++it)
{
if(!viz[it->first])
{
viz[curent.first] = 1;
ok = 1;
c[++end] = *it;
if(it->first > curent.first)
dist[it->first] = dist[curent.first] + it->second;
else
dist[it->first] = dist[curent.first] - it->second;
}
}
}
if(dist[Y] < 0) dist[Y] *= -1;
printf("%d", dist[Y]);
return 0;
}