Pagini recente » Cod sursa (job #879827) | Cod sursa (job #1263342) | Cod sursa (job #2077962) | Cod sursa (job #1829106) | Cod sursa (job #1194430)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream is("sate.in");
ofstream os("sate.out");
#define DIM 30001
int N, M, X, Y, x, y, c;
bool Vis[DIM];
int R[DIM];
vector <pair<int,int> > V[DIM];
queue <int> Q;
int main()
{
is >> N >> M >> X >> Y;
for ( int i = 1; i <= M; ++i )
{
is >> x >> y >> c;
V[x].push_back(make_pair(y,c));
V[y].push_back(make_pair(x,c*(-1)));
}
Q.push(X); int nod; Vis[X] = true;
while ( !Q.empty() )
{
nod = Q.front();
Q.pop();
for ( int i = 0; i < V[nod].size(); ++i )
{
if ( Vis[V[nod][i].first] == false )
{
R[V[nod][i].first] = R[nod] + V[nod][i].second;
Q.push(V[nod][i].first);
Vis[V[nod][i].first] = true;
if ( V[nod][i].first == Y )
{
os << R[V[nod][i].first];
return;
}
}
}
}
return 0;
}