Pagini recente » Cod sursa (job #1863042) | Cod sursa (job #1555924) | Cod sursa (job #120419) | Cod sursa (job #1715673) | Cod sursa (job #645326)
Cod sursa(job #645326)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in("sate.in");
ofstream out ("sate.out");
int m,n,s,f,dist[30002];
vector <int> a[30002],d[30002];
queue <int> q;
void citire () {
in >> n >> m >> s >> f;
int x,y,euh;
while (m--){
in >> x >> y >> euh;
a[x].push_back(y);
a[y].push_back(x);
d[y].push_back(euh);
d[x].push_back(euh);
}
for (int i = 1; i <= n; ++i) {
dist[i] = -1;
}
}
void bfs () {
q.push(s);
dist[s] = 0;
int x,y;
while(!q.empty()) {
x = q.front();
q.pop();
for (size_t i = 0; i < a[x].size(); ++i) {
y = a[x][i];
if (dist[y] != -1){
continue;
}
q.push(y);
if (x > y) {
dist[y] = dist[x] - d[x][i];
} else {
dist[y] = dist[x] + d[x][i];
}
}
}
}
int main () {
citire();
bfs();
out << dist[f];
return 0;
}