Pagini recente » Cod sursa (job #793917) | Cod sursa (job #1716558) | Cod sursa (job #1445968) | Cod sursa (job #302699) | Cod sursa (job #638893)
Cod sursa(job #638893)
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#define NMAX 30001
using namespace std;
typedef vector< pair<int, int> > lista;
vector<lista> a(NMAX);
queue< pair<int, int> > c;
int viz[NMAX], dist[NMAX];
int N, M, X, Y;
int main()
{
pair<int, int>curent;
int i, x, y, d, ok, yt;
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.push(make_pair(X, 0));
while(!c.empty())
{
curent = c.front();
c.pop();
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])
{
ok = 1;
c.push(*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;
}