Pagini recente » Cod sursa (job #1568948) | Cod sursa (job #1796307) | Cod sursa (job #634964) | Cod sursa (job #447007) | Cod sursa (job #1253343)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
const int MAXN = 30005;
int N, M, X, Y;
vector <pair<int, int> > g[MAXN];
ifstream fin("sate.in");
ofstream fout("sate.out");
int main() {
fin >> N >> M >> X >> Y;
while(M --) {
int x, y, z;
fin >> x >> y >> z;
g[x].push_back(make_pair(y, z));
g[y].push_back(make_pair(x, -z));
}
vector <int> dp(N + 1, 1 << 30);
dp[X] = 0;
queue <int> q;
q.push(X);
while(!q.empty()) {
int node = q.front();
for(auto it : g[node]) {
if(dp[it.first] > dp[node] + it.second) {
dp[it.first] = dp[node] + it.second;
q.push(it.first);
}
}
q.pop();
}
fout << dp[Y] << '\n';
}