Pagini recente » Cod sursa (job #274157) | Cod sursa (job #3178408) | Cod sursa (job #1530571) | Cod sursa (job #1542205) | Cod sursa (job #1116009)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream fin( "sate.in" );
ofstream fout( "sate.out" );
struct muchie { int nod, cost; };
const int nmax = 30000;
bool p[nmax+1];
int l[nmax+1];
vector <muchie> v[nmax+1];
inline muchie str_ret ( int b, int c ) {
muchie sol;
sol.nod = b;
sol.cost = c;
return sol;
}
void bfs( int n ) {
queue <int> q;
p[n] = 1;
q.push( n );
while ( !q.empty() ) {
int k = q.front();
q.pop();
for ( int i = 0; i < (int)v[k].size(); ++ i ) {
if ( p[ v[k][i].nod ] == 0 ) {
p[ v[k][i].nod ] = 1;
l[ v[k][i].nod ] = l[k] + v[k][i].cost;
q.push( v[k][i].nod );
}
}
}
}
int main()
{
int n, m, x, y, a, b, c;
fin>>n>>m>>x>>y;
for ( int i = 0; i < m; ++ i ) {
fin>>a>>b>>c;
v[a].push_back( str_ret( b, -c ) );
v[b].push_back( str_ret( a, c ) );
}
bfs( x );
fout<<l[y]<<'\n';
}