Pagini recente » Cod sursa (job #2679180) | Cod sursa (job #880004) | Cod sursa (job #998895) | Cod sursa (job #2575629) | Cod sursa (job #1086007)
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#define PII pair<int, int>
#define x first
#define y second
using namespace std;
const int N=30005;
ifstream fin("sate.in");
ofstream fout("sate.out");
vector <PII> G[N];
int d[N];
int main()
{
int n, m, i, x, y, z, X, Y;
queue <int> Q;
fin>>n>>m>>X>>Y;
while(m--)
{
fin>>x>>y>>z;
G[x].push_back(make_pair(y, z));
G[y].push_back(make_pair(x, z));
}
memset(d, -1, sizeof(d));
d[X]=0;
for(Q.push(X);!Q.empty()&&d[Y]==-1;Q.pop())
{
x=Q.front();
for(auto p: G[x])
{
if(d[p.x]==-1)
{
d[p.x]=x<p.x?d[x]+p.y:d[x]-p.y;
Q.push(p.x);
}
}
}
fout<<d[Y];
fin.close();
fout.close();
}