Pagini recente » Cod sursa (job #1072247) | Cod sursa (job #1925571) | Cod sursa (job #2956444) | Cod sursa (job #2990966) | Cod sursa (job #1077071)
#include <algorithm>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
//#define FORIT(it, v) for (typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
const int N = 30005;
int n, m, x, y;
int d[N];
bool vis[N];
vector <pair <int, int> > graph[N];
queue <int> q;
void read() {
f >> n >> m >> x >> y;
for (int i = 1; i <= m; ++i) {
int a, b, c;
f >> a >> b >> c;
graph[a].push_back(make_pair(b, c));
graph[b].push_back(make_pair(a, -c));
}
}
void bfs(int x) {
q.push(x);
while (!q.empty()) {
int node = q.front();
for (vector <pair <int, int> >::iterator it = graph[node].begin(); it != graph[node].end(); ++it) {
if (!vis[it->first]) {
vis[it->first] = true;
q.push(it->first);
d[it->first] = d[node] + it->second;
}
}
q.pop();
}
g << d[y];
}
int main() {
read();
bfs(x);
}