Pagini recente » Cod sursa (job #1835544) | Cod sursa (job #2003206) | Cod sursa (job #2444304) | Cod sursa (job #469531) | Cod sursa (job #1053464)
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
#define Nmax 100025
int cost[Nmax], N, M, X, Y;
vector<pair<int, int> > G[Nmax];
void BFS(int start) {
queue<int> Q;
int dist, nod, x;
Q.push(start);
while(!Q.empty()) {
x=Q.front();
for(vector<pair<int, int> >:: iterator it=G[x].begin(), it2=G[x].end(); it!=it2; ++it) {
nod=it->first; dist=it->second;
if(!cost[nod]) {
cost[nod]=cost[x]+dist;
if(nod==Y)
return;
Q.push(nod);
}
}
Q.pop();
}
}
int main() {
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
int x, y, c;
scanf("%d %d %d %d",&N,&M,&X,&Y);
while(M--) {
scanf("%d %d %d",&x,&y,&c);
G[x].push_back(make_pair(y,c));
G[y].push_back(make_pair(x,-c));
}
BFS(X);
printf("%d\n",cost[Y]);
return 0;
}