Pagini recente » Cod sursa (job #477577) | Cod sursa (job #1709774) | Cod sursa (job #1884297) | Cod sursa (job #2332065) | Cod sursa (job #1309380)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in( "sate.in" );
ofstream out( "sate.out" );
const int NMAX= 30000;
struct GRAF
{
int vf, cost;
};
vector <GRAF> d[NMAX+1];
queue <GRAF> q;
GRAF nr;
bool viz[NMAX+1];
int D[NMAX+1];
int main()
{
int N, M, X, S, a, b, c;
in >> N >> M >> X >> S;
if( X>S )
{
int ff= X;
X= S;
S= ff;
}
for( int i= 1; i<=M; ++i )
{
in >> a >> b >> c;
nr.vf= b;
nr.cost= c;
d[a].push_back( nr );
nr.vf= a;
d[b].push_back( nr );
}
//init
nr.vf= X;
nr.cost= 0;
D[ X ]= 0;
viz[X]= 1;
//init
//DFS
int x, xcost;
q.push(nr);
while( !q.empty() )
{
GRAF aux= q.front();
int lg= (int)d[aux.vf].size();
for( int i= 0; i<lg; ++i )
{
x= d[aux.vf][i].vf;
xcost= d[aux.vf][i].cost;
if( viz[x]==0 )
{
if( aux.vf < x )
{
D[x]= D[aux.vf] + xcost;
}
else D[x]= D[aux.vf] - xcost;
q.push( d[aux.vf][i] );
viz[x]= 1;
}
}
q.pop();
}
//DFS
out << D[S] << '\n';
return 0;
}