Pagini recente » Cod sursa (job #280122) | Cod sursa (job #1557707) | Cod sursa (job #777551) | Cod sursa (job #3243527) | Cod sursa (job #1194381)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream is("sate.in");
ofstream os("sate.out");
int n, m, x, y;
vector<vector<pair<int, int> > > c;
vector<bool> ok;
queue<pair<int, int> > q;
void READ();
int main()
{
READ();
q.push(make_pair(x, 0));
int w, d;
pair<int, int> nod;
while ( !q.empty() )
{
w = q.front().first;
d = q.front().second;
q.pop();
if ( w == y )
{
os << d;
break;
}
for ( vector<pair<int, int> >::iterator it = c[w].begin(); it != c[w].end(); ++it )
{
nod = *it;
if ( ok[nod.first] )
continue;
ok[nod.first] = true;
if ( w > nod.first )
q.push(make_pair(nod.first, d - nod.second));
else
q.push(make_pair(nod.first, d + nod.second));
}
}
is.close();
os.close();
return 0;
}
void READ()
{
is >> n >> m >> x >> y;
int i, j, d;
c.resize(n + 1);
ok.resize(n + 1);
while ( m-- )
{
is >> i >> j >> d;
c[i].push_back(make_pair(j, d));
c[j].push_back(make_pair(i, d));
}
}