Pagini recente » Cod sursa (job #744887) | Cod sursa (job #2080362) | Cod sursa (job #2797865) | Cod sursa (job #1268978) | Cod sursa (job #1253575)
#include<fstream>
#include<iostream>
using namespace std;
int ind_stiva,n,s[300],viz[300],a[300][300],x,y,i,j,k;
long dist,m;
ifstream f("sate.in");
ofstream g("sate.out");
void intro()
{
int d;
f>>n>>m>>x>>y;
for(i=1;i<=m;i++)
{
f>>j>>k>>d;
if(k<j)
{
a[k][j]=-d;
a[j][k]=d;
}
else
{
a[j][k]=d;
a[k][j]=-d;
}
}
}
void push(int j)
{
s[ind_stiva++]=j;
}
int pop()
{
return (s[ind_stiva--]);
}
int urmator(int i)
{
int l;
for(l=1;l<=n;l++)
if(a[i][l]!=0 && !viz[l] && l!=i) return l ;
return 0 ;
}
int main()
{
int ok=0;
intro();
for(i=1;i<-n;i++)
viz[i]=0;
j=x;
viz[j]=1;
push(j);
do{
if(ok==0)
{
k=urmator(j);
if(k==y)
{
ok=1;
}
if(k>0 && ok==0)
{
viz[k]=1;
push(j=k);
}
else
{
if(ok==1)
dist=a[s[ind_stiva-1]][y];
j=s[ind_stiva--];
}
}
else
{
dist=dist+a[s[ind_stiva-1]][s[ind_stiva]];
j=s[ind_stiva--];
}
}while(ind_stiva>=0) ;
g<<dist;
g.close();
return 0;
}