Pagini recente » Cod sursa (job #935515) | Cod sursa (job #771968) | Cod sursa (job #2110297) | Cod sursa (job #757739) | Cod sursa (job #796730)
Cod sursa(job #796730)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
bool visited[30001];
vector<pair<int, int> >g[30001];
queue<int> q;
int c[30001], n, m, x, y, n1, n2, cost;
void Solve(int node, int destination)
{
int current = node;
q.push(current);
while ( !q.empty())
{
current = q.front();
for ( size_t i = 0; i < g[current].size(); i++ )
{
if ( c[current]+g[current][i].second < c[g[current][i].first] )
{
c[g[current][i].first]=c[current]+g[current][i].second;
q.push(g[current][i].first);
if ( g[current][i].first == destination )
{
return;
}
}
}
q.pop();
}
}
int main()
{
fin >> n >> m >> x >> y;
for ( int i = 1; i <= m; i++ )
{
fin >> n1 >> n2 >> cost;
g[n1].push_back(make_pair(n2,cost));
g[n2].push_back(make_pair(n1,-cost));
}
for ( int i = 1; i <= n; i++ )
c[i] = 999999999;
c[x] = 0;
Solve(x,y);
fout<<c[y];
return 0;
}