Pagini recente » Cod sursa (job #1047664) | Cod sursa (job #1049192) | Cod sursa (job #1858792) | Cod sursa (job #2932632) | Cod sursa (job #329940)
Cod sursa(job #329940)
#include <fstream.h>
#define MaxN 30009
#define MaxM 100050
int s[MaxN],T[2][2*MaxM],cost[2*MaxM],d[MaxN],c[MaxN],n,m,X,Y;
void add(int x,int y,int t,int &st)
{
T[0][st]=y;
T[1][st]=s[x];
cost[st]=t;
s[x]=st;
st++;
}
void cit()
{
int i,x,y,t,st=1;
ifstream fin("sate.in");
fin>>n>>m>>X>>Y;
for(i=1;i<=m;i++)
{
fin>>x>>y>>t;
add(x,y,t,st);
add(y,x,t,st);
}
fin.close();
}
void BFS(int nod)
{
int st=1,p,i,sw=1;
c[1]=nod;
d[nod]=1;
for(i=1;i<=st && sw;i++)
{
p=s[c[i]];
while(p && sw)
{
if(!d[T[0][p]])
{
st++;
c[st]=T[0][p];
if(c[i]>T[0][p])
d[T[0][p]]=d[c[i]]-cost[p];
else
d[T[0][p]]=d[c[i]]+cost[p];
if(T[0][p]==Y)
sw=0;
}
p=T[1][p];
}
}
}
void afis()
{
ofstream fout("sate.out");
d[Y]--;
fout<<d[Y];
fout.close();
}
int main()
{
cit();
BFS(X);
afis();
return 0;
}