Pagini recente » Cod sursa (job #2656430) | Cod sursa (job #1236288) | Cod sursa (job #2278390) | Cod sursa (job #1794027) | Cod sursa (job #863290)
Cod sursa(job #863290)
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stdio.h>
using namespace std;
#define inf 21000000
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;
scanf("%d %d %d %d",&n,&m,&x,&y);
graf.resize(n+1);
cost.resize(n+1, inf);
vizitat.resize(n+1);
for(int i = 1; i <= m; ++i){
scanf("%d %d %d",&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);
}
fclose(stdin);
}
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;
if(it->nod == y){
printf("%u",cost[y]);
return;
}
}
coada.pop();
first = coada.front();
}
}
int main(){
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
citire();
bfs();
fclose(stdout);
return 0;
}