Pagini recente » Cod sursa (job #2868013) | Cod sursa (job #1159287) | Cod sursa (job #2700063) | Cod sursa (job #2587644) | Cod sursa (job #863282)
Cod sursa(job #863282)
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
#include <queue>
using namespace std;
#define inf 21000000
ifstream f ("sate.in");
ofstream g ("sate.out");
struct nod_g{
int nod, cost;
};
vector < list<nod_g> > graf;
vector <unsigned int> cost;
vector <bool> vizitat;
list <nod_g>::iterator it;
int n, m, x, y;
nod_g temp;
void citire(){
int x1, y1, c;
f >> n >> m >> x >> y;
graf.resize(n+1);
cost.resize(n+1, inf);
vizitat.resize(n+1);
for(int i = 1; i <= m; ++i){
f >> x1 >> y1 >> c;
temp.nod = y1; temp.cost = c;
graf[x1].push_back(temp);
temp.nod = x1; temp.cost = -c;
graf[y1].push_back(temp);
}
f.close();
}
void bfs(){
int first = x;
queue <unsigned int> coada;
coada.push(x);
cost[x] = 0;
vizitat[x] = 1;
while(!coada.empty()){
for(it = graf[first].begin(); it != graf[first].end(); ++it)
if(!vizitat[it->nod]){
vizitat[it->nod] = 1;
coada.push(it->nod);
cost[it->nod] = cost[first] + it->cost;
}
coada.pop();
first = coada.front();
}
}
int main(){
citire();
bfs();
if( cost[y] != inf)
g << cost[y];
g.close();
return 0;
}