Pagini recente » Clasament rmi30000 | Cod sursa (job #210349) | Cod sursa (job #1600112) | Cod sursa (job #842977) | Cod sursa (job #1006636)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
struct muchie {
int sat,dist;
};
vector <muchie> v[30005];
queue <int> q;
int dist[30005];
int n,m,x,y;
muchie mk_muchie(int a,int d) {
muchie res;
res.sat = a;
res.dist = d;
return res;
}
inline void bfs() {
dist[x] = 1;
q.push(x);
while (!q.empty() && dist[y] == 0) {
int n = q.front(); q.pop();
while (!v[n].empty()) {
muchie crt = v[n].back(); v[n].pop_back();
int nod = crt.sat;
if (dist[nod] > 0) continue;
if (nod > n) dist[nod] = dist[n] + crt.dist;
else dist[nod] = dist[n] - crt.dist;
q.push(nod);
}
}
}
int main() {
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d %d %d %d",&n,&m,&x,&y);
for (int i=1;i<=m;i++) {
int a,b,d;
scanf("%d %d %d",&a,&b,&d);
v[a].push_back(mk_muchie(b,d));
v[b].push_back(mk_muchie(a,d));
}
bfs();
printf("%d\n",dist[y]-1);
return 0;
}