Pagini recente » Borderou de evaluare (job #366908) | Cod sursa (job #887921) | Cod sursa (job #356580) | Cod sursa (job #2366948) | Cod sursa (job #1113743)
//horatiu11
# include <cstdio>
# include <vector>
# include <queue>
# define nmax 30001
using namespace std;
int n,m,x,y,a,b,d,viz[nmax];
vector < pair<int,int> >G[nmax];
queue <int>q;
inline void BFS(int x)
{
int i;
q.push(x);viz[x]=1;
while(!q.empty() && !viz[y])
{
x=q.front();q.pop();
for(i=0;i<G[x].size();++i)
if(!viz[G[x][i].first] )//|| viz[G[x][i].first]>viz[x]+G[x][i].second)
{
q.push(G[x][i].first);
if(x>G[x][i].first)viz[G[x][i].first]=viz[x]-G[x][i].second;
else viz[G[x][i].first]=viz[x]+G[x][i].second;
}
}
}
int main()
{
int i;
freopen("sate.in","r",stdin);
freopen("sate.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&x,&y);
for(i=1;i<=m;++i)
{
scanf("%d%d%d",&a,&b,&d);
G[a].push_back(make_pair(b,d));
G[b].push_back(make_pair(a,d));
}
BFS(x);
printf("%d\n",viz[y]-1);
return 0;
}