Pagini recente » Cod sursa (job #27200) | Cod sursa (job #1045120) | Istoria paginii runda/nr92/clasament | Cod sursa (job #1983622) | Cod sursa (job #1503933)
#include <iostream>
#include <vector>
#include <string.h>
#include <fstream>
#include <queue>
using namespace std;
const int maxn = 100005;
int n, m, x, y, dist[maxn];
vector<pair<int, int> > g[maxn];
int main()
{
ifstream fin("sate.in");
ofstream fout("sate.out");
fin >> n >> m >> x >> y;
for(int i = 1 ; i <= m ; ++ i) {
int x, y, z;
fin >> x >> y >> z;
g[x].push_back(make_pair(y, z));
g[y].push_back(make_pair(x,-z));
}
memset(dist, -1, sizeof(dist));
queue <int> q;
q.push(x);
dist[x] = 0;
while(!q.empty()) {
int node = q.front();
q.pop();
for(auto el : g[node]) {
if(dist[el.first] == -1) {
dist[el.first] = dist[node] + el.second;
q.push(el.first);
}
}
}
fout << dist[y] << '\n';
return 0;
}