Pagini recente » Cod sursa (job #2578952) | Cod sursa (job #2579481) | Cod sursa (job #2571572) | Cod sursa (job #1390532) | Cod sursa (job #1457307)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
int find_dist(const vector<vector<pair<int, int> > >& graf,
const int x, const int y){
vector<int> dist(graf.size(), -1);
dist[x] = 0;
queue<int> de_viz;
de_viz.push(x);
while(dist[y] == -1){
const int cur = de_viz.front();
de_viz.pop();
for(const auto next : graf[cur]){
if(dist[next.first] == -1){
dist[next.first] = dist[cur] + next.second;
de_viz.push(next.first); } } }
return dist[y]; }
int main(){
ifstream f("sate.in");
ofstream g("sate.out");
int n, m, x, y;
f >> n >> m >> x >> y;
--x, --y;
vector<vector<pair<int, int> > > v(n);
for(int i = 0, a, b, c; i < m; ++i){
f >> a >> b >> c;
--a, --b;
v[a].emplace_back(b, c);
v[b].emplace_back(a, -c); }
g << find_dist(v, x, y);
return 0; }