Pagini recente » Cod sursa (job #1851475) | Cod sursa (job #1513057) | Cod sursa (job #2216606) | Cod sursa (job #2913466) | Cod sursa (job #3290385)
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
const int NMAX = 3e4 + 5;
vector<pair<int,int> > G[NMAX];
int cost[NMAX];
void bfs(int nod){
queue<int> q;
q.push(nod);
cost[nod] = 0;
while(!q.empty()){
int nod_curent = q.front();
q.pop();
for (int i = 0; i < G[nod_curent].size(); i++){
int nod_nou = G[nod_curent][i].first;
if(cost[nod_nou] == 0){
cost[nod_nou] = cost[nod_curent] + G[nod_curent][i].second;
q.push(nod_nou);
}
}
}
}
int main(){
int N, M, start, final;
fin >> N >> M >> start >> final;
while(M--){
int x, y, d;
fin >> x >> y >> d;
G[x].push_back(make_pair(y, d));
G[y].push_back(make_pair(x, -d));
}
bfs(start);
fout << cost[final];
}