Pagini recente » Cod sursa (job #2689054) | Cod sursa (job #1789816) | Cod sursa (job #2646729) | Cod sursa (job #2564514) | Cod sursa (job #2553866)
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;
#ifdef DEBUG
string name = "data";
#else
string name = "sate";
#endif
ifstream fin(name + ".in");
ofstream fout(name + ".out");
int n, m;
int s, t;
#define MAXN 30005
struct Edge {
int to;
int weight;
};
vector<Edge> g[MAXN];
int d[MAXN];
bool viz[MAXN];
int main() {
fin >> n >> m >> s >> t;
for (int i = 0; i < m; ++i) {
int x,y,w;
fin >> x >> y >> w;
g[x].push_back({y, w});
g[y].push_back({x, -w});
}
queue<int> q;
q.push(s);
viz[s] = true;
while (true) {
auto node = q.front();
q.pop();
for (auto edge: g[node]) {
if (viz[edge.to]) {
continue;
}
viz[edge.to] = true;
d[edge.to] = d[node] + edge.weight;
q.push(edge.to);
if (edge.to == t) {
fout << d[edge.to];
return 0;
}
}
}
return 0;
}