Pagini recente » Cod sursa (job #2558041) | Cod sursa (job #1840851) | Cod sursa (job #553004) | Cod sursa (job #2369475) | Cod sursa (job #907290)
Cod sursa(job #907290)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("sate.in");
ofstream out ("sate.out");
const int MAXN = 30010;
vector < pair <int, int> > Graf[MAXN];
queue <int> Q;
int Cost[MAXN];
bool Viz[MAXN];
int main()
{
int N, M, X, Y, a, b, c, i;
vector < pair <int, int> > :: iterator it;
int nod;
in >> N >> M >> X >> Y;
while (M --){
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 ++){
Viz[i] = 0;
Cost[i] = (1 << 30);
}
Q.push (X);
Cost[X] = 0;
while (!Q.empty ()){
nod = Q.front ();
Q.pop ();
Viz[nod] = 0;
for (it = Graf[nod].begin (); it != Graf[nod].end (); ++ it)
if (Cost[nod] + (it -> second) < Cost[it -> first]){
Cost[it -> first] = Cost[nod] + (it -> second);
if (it -> first == Y){
out << Cost[it -> first];
return 0;
}
if (!Viz[it -> first]){
Q.push (it -> first);
Viz[it -> first] = 1;
}
}
}
return 0;
}